Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Class 如何在vbscript中的类内使用词典?_Class_Oop_Vbscript - Fatal编程技术网

Class 如何在vbscript中的类内使用词典?

Class 如何在vbscript中的类内使用词典?,class,oop,vbscript,Class,Oop,Vbscript,我试图在vbscript中创建一个类,该类的成员变量之一是字典,并为其Add方法创建一个简单的包装器类。事情真的非常糟糕。我已经注释掉了所有不起作用的行 Class ConfigSection Private m_Name ' A dictionary of values to set {Section:{name:value, name2:value2}} Private m_Values Private m_Overwrite Public Func

我试图在vbscript中创建一个类,该类的成员变量之一是字典,并为其Add方法创建一个简单的包装器类。事情真的非常糟糕。我已经注释掉了所有不起作用的行

Class ConfigSection
    Private m_Name
    ' A dictionary of values to set {Section:{name:value, name2:value2}}
    Private m_Values
    Private m_Overwrite

    Public Function init(p_Name, p_Overwrite)
        Set init = Me
        m_Name = p_name
        m_Overwrite = p_Overwrite
        'Values = CreateObject("Scripting.Dictionary")
    End Function

    Public Property Get Name
        Name = m_Name
    End Property

    Public Property Get Overwrite
        Overwrite = m_Overwrite
    End Property

    Public Sub Add(Name, Value)
        'Values().Add Name, Value
    End Sub

    'Private Property Let Values(Value)
    '   Set m_Values = Value
    'End Property

End Class

不建议通过公开成员变量来公开它们。要公开字典,请正确包装您希望在类的属性和方法中访问的那些属性和方法

允许添加、更改和删除键/值对以及检查是否存在键的示例:

Class Foo
  Private d_

  Private Sub Class_Initialize
    Set d_ = CreateObject("Scripting.Dictionary")
  End Sub

  Public Property Let Item(name, value)
    d_(name) = value
  End Property

  Public Property Get Item(name)
    Item = d_(name)
  End Property

  Public Function Exists(name)
    Exists = d_.Exists(name)
  End Function

  Public Sub Remove(name)
    d_.Remove(name)
  End Sub
End Class

Set obj = New Foo

WScript.Echo "" & obj.Exists("bar")
obj.Item("bar") = 42
WScript.Echo "" & obj.Exists("bar")

WScript.Echo obj.Item("bar")

obj.Remove("bar")
WScript.Echo "" & obj.Exists("bar")

不建议通过公开成员变量来公开它们。要公开字典,请正确包装您希望在类的属性和方法中访问的那些属性和方法

允许添加、更改和删除键/值对以及检查是否存在键的示例:

Class Foo
  Private d_

  Private Sub Class_Initialize
    Set d_ = CreateObject("Scripting.Dictionary")
  End Sub

  Public Property Let Item(name, value)
    d_(name) = value
  End Property

  Public Property Get Item(name)
    Item = d_(name)
  End Property

  Public Function Exists(name)
    Exists = d_.Exists(name)
  End Function

  Public Sub Remove(name)
    d_.Remove(name)
  End Sub
End Class

Set obj = New Foo

WScript.Echo "" & obj.Exists("bar")
obj.Item("bar") = 42
WScript.Echo "" & obj.Exists("bar")

WScript.Echo obj.Item("bar")

obj.Remove("bar")
WScript.Echo "" & obj.Exists("bar")

逐一浏览注释行:


将对象引用指定给变量时,需要使用
Set
语句


您需要为
值定义
属性Get
,然后才能访问它。否则,它是一个只写属性


由于此属性包含对象引用,因此必须使用关键字
属性集


总而言之:

Class ConfigSection
    Private m_Name
    ' A dictionary of values to set {Section:{name:value, name2:value2}}
    Private m_Values
    Private m_Overwrite

    Public Function init(p_Name, p_Overwrite)
        Set init = Me
        m_Name = p_name
        m_Overwrite = p_Overwrite
        Set Values = CreateObject("Scripting.Dictionary")
    End Function

    Public Property Get Name
        Name = m_Name
    End Property

    Public Property Get Overwrite
        Overwrite = m_Overwrite
    End Property

    Public Sub Add(Name, Value)
        Values().Add Name, Value
    End Sub

    Private Property Get Values
        Set Values = m_Values
    End Property

    Private Property Set Values(Value)
       Set m_Values = Value
    End Property

End Class

逐一浏览注释行:


将对象引用指定给变量时,需要使用
Set
语句


您需要为
值定义
属性Get
,然后才能访问它。否则,它是一个只写属性


由于此属性包含对象引用,因此必须使用关键字
属性集


总而言之:

Class ConfigSection
    Private m_Name
    ' A dictionary of values to set {Section:{name:value, name2:value2}}
    Private m_Values
    Private m_Overwrite

    Public Function init(p_Name, p_Overwrite)
        Set init = Me
        m_Name = p_name
        m_Overwrite = p_Overwrite
        Set Values = CreateObject("Scripting.Dictionary")
    End Function

    Public Property Get Name
        Name = m_Name
    End Property

    Public Property Get Overwrite
        Overwrite = m_Overwrite
    End Property

    Public Sub Add(Name, Value)
        Values().Add Name, Value
    End Sub

    Private Property Get Values
        Set Values = m_Values
    End Property

    Private Property Set Values(Value)
       Set m_Values = Value
    End Property

End Class
Class ConfigSection
    Private m_Name
    ' A dictionary of values to set {Section:{name:value, name2:value2}}
    Private m_Values
    Private m_Overwrite

    Public Function init(p_Name, p_Overwrite)
        Set init = Me
        m_Name = p_name
        m_Overwrite = p_Overwrite
        Set Values = CreateObject("Scripting.Dictionary")
    End Function

    Public Property Get Name
        Name = m_Name
    End Property

    Public Property Get Overwrite
        Overwrite = m_Overwrite
    End Property

    Public Sub Add(Name, Value)
        Values().Add Name, Value
    End Sub

    Private Property Get Values
        Set Values = m_Values
    End Property

    Private Property Set Values(Value)
       Set m_Values = Value
    End Property

End Class