Dynamically add a property?

Suamere

Member
Joined
Aug 23, 2012
Messages
11
Programming Experience
5-10
I'd like to be able to do this:

Dim objClass As New ClassName

objClass.Add("PropertyClass")

objClass.PropertyClass.Property = "abcd"
objClass.PropertyClass.Other = "lol"
objClass.propertyClass.Numbers = 1234

Or any other ideas for dual layers, like maybe:

objClass("PropertyClass").Property = "abcd"
objClass("PropertyClass").Other = "lol"

~Suamere
 
Nevermind, I'm tired and completely missed my own point. I just need to make two classes and make base class properties create new instances of the second class.
 
To fully close this, I'll post the code I used:

Public Class MainClass
  Public Sub Main()
    Dim MyInstance1 As New clsClass
    Dim MyInstance2 As New clsClass

    MyInstance1("Name").Value = "Sua"
    MyInstance1("Age").Value = "30"
    MyInstance1("Height").Value = "6'2"

    MyInstance2("Name").Value = "Scalar"
    MyInstance2("Age").Value = "590"
    MyInstance2("Height").Value = "6'1"

  End Sub
End Class
 
Public Class clsClass
  Private oPC2 As New Dictionary(Of String, clsClass2)
 
  Default Property P(ByVal strString As String) As clsClass2
    Get
      If oPC2.ContainsKey(strString) = False Then _
        oPC2.Add(strString, New clsClass2)
      Return oPC2(strString)
    End Get
    Set(ByVal value As clsClass2)
    End Set
  End Property
End Class
 
Public Class clsClass2
  Public Property Value As String
End Class


This is obviously very similar to a dictionary, except I can add more than "value" as properties, in my case I have masks, lists, recursive dictionaries, and more. And since I control the class, every one of these properties have a great deal of customization through the get/set options. So it's like having a Super Dictionary with multiple levels of values. I have a question about a capability though which I'll bring up in a new post since it is slightly unrelated.
 
Back
Top