首页 文章

VBScript变量丢失其内容?

提问于
浏览
1

我有以下代码 . 它的作用是从属性文件中读取所有关键值 .

Function readProperties(fileName)
    Dim oFS : Set oFS = CreateObject( "Scripting.FileSystemObject" )
    Dim dicProps : Set dicProps = CreateObject( "Scripting.Dictionary" )
    Dim oTS : Set oTS = oFS.OpenTextFile( fileName )
        Do Until oTS.AtEndOfStream
            Dim sLine : sLine = Trim( oTS.ReadLine )
            'Wscript.Echo sLine
            If ""  sLine Then
                If not "#" = Left( sLine, 1 ) Then

                    Dim aParts : aParts = Split( sLine, "=" )
                    If 1  UBound( aParts ) Then
                        WScript.Echo oTS.Line, "bad property line", sLine
                    Else
                    'Wscript.Echo "Adding: " & aParts( 0 ) &" => " & aParts( 1 )
                        dicProps( Trim( aParts( 0 ) ) )  = Trim( aParts( 1 ) ) 
                    'WScript.Echo oTS.Line, "good property line",  sLine
                    End If

                End If
            End If
        Loop
    oTS.Close

    'readProperties = dicProps

    Dim sKey
    For Each sKey In dicProps.Keys
        WScript.Echo sKey, "=>", dicProps( sKey )
    Next
End Function

奇怪的是,如果我将 dicProps 的值赋给 readProperties ,则代码不再起作用 .

我错过了什么吗?

1 回答

  • 2

    使用

    Set readProperties = dicProps
    

    在处理对象时,您总是必须使用Set .

相关问题