1

I'm trying to create a class in vbscript that has a dictionary as one of its member variables and create a simple wrapper class for its Add method. It's really going pretty terribly. I've commented out all the lines that aren't working.

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
0

2 Answers 2

4

Exposing member variables by making them public is not recommended. To expose a dictionary properly wrap those of its properties and methods that you want accessible in properties and methods of your class.

Example allowing adding, changing, and deleting key/value pairs as well as checking for the presence of a key:

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")
0
1

Going through the commented lines one by one:


Values = CreateObject("Scripting.Dictionary")

You need to use the Set statement when assigning object references to variables.


Values().Add Name, Value

You need to define a Property Get for Values before you can access it. Otherwise, it is a write-only property.


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

Since this property contains an object reference, you must use the keyword Property Set.


Putting it all together:

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.