Creating a dynamic class of classes

I've had a mental block on this issue for like 2 days now...I need help getting over the hump.  Basically, I need to create a class of unknown number of classes.  

So let's say I have a class like this:
Public Class clsCommon
    Private m_Field1 As String = String.Empty
    Public Property Field1() As String
        Get
            Return m_Field1
        End Get
        Set(ByVal value As String)
            m_Field1 = value.ToString
        End Set
    End Property
    Private m_Field2 As String = String.Empty
    Public Property Field2() As String
        Get
            Return m_Field2
        End Get
        Set(ByVal value As String)
            m_Field2 = value.ToString
        End Set
    End Property
End Class

Now I want to create a class made up of multiple instances of clsCommon.  I know the easiest way to do that would be something like this:
Public Class clsMain
    Private m_clsOne As clsCommon = Nothing
    Public Property clsOne() As clsCommon
        Get
            Return m_clsOne
        End Get
        Set(ByVal value As clsCommon)
            m_clsOne = value.ToString
        End Set
    End Property

    Private m_clsTwo As clsCommon = Nothing
    Public Property clsTwo() As clsCommon
        Get
            Return m_clsTwo
        End Get
        Set(ByVal value As clsCommon)
            m_clsTwo = value.ToString
        End Set
    End Property
End Class

But I want to be able to have a dynamic number of clsCommon's.  I don't want to "hard code" them.  
LVL 4
Steve SiricaSenior Web Application EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Steve SiricaSenior Web Application EngineerAuthor Commented:
Let me further explain what I'm really trying to accomplish.  We have this application written in VB6 that uses 2 types of input: a flat file containing various records types or data from a database with multiple tables that correspond to the different record types.  The origional app will take either of these and convert them into a big array and then process the records accordingly.  

Now that we live in a "real time" world, our customers don't want to use a batch process anymore.  They want a real time solution, so I'm converting this to a web service that will use an XML structure as it's input.  The problem is that the return structure has to contain the same core values plus all the calculated new values derived from varioius default values and what not.  So if I pass in something like this:
<Input>
    <Common>
        <Code>STD</Code>
        <Desc></Desc>
    </Common>
    <Other>
        <Type>OTH</Type>
        <Value></Value>
    </Other>
    <Other>
        <Type>XXX</Type>
        <Value></Value>
    </Other>
</Input>

the new web service would return:
<Return>
    <Common>
        <Code>STD</Code>
        <Desc>Standard Code</Desc>
    </Common>
    <Other>
        <Type>OTH</Type>
        <Value>Some value associate with OTH</Value>
    </Other>
    <Other>
        <Type>XXX</Type>
        <Value>Some value associate with XXX</Value>
    </Other>
</Return>

Within the web service each upper level XML element has a corresponding class with properties that correspond to the lower level XML elements.  Each class also contains functions specific to it's type to handle processing.

The indiviudual clasees and the processing element I already have handled...it's the importing of the XML structure into the classes that I'm stuct on, bacuse there can be more than one of each type.  

I hope that helps.

0
Göran AnderssonCommented:
There are several generic collections that you can use with your class, like List and Dictionary.

First, let's add a constructor in the clsCommon class so that you can easily populate the objects when you create them:

Public Class clsCommon
    Private m_Field1 As String = String.Empty
    Private m_Field2 As String = String.Empty
    Public Sub New(field1 As String, field2 As String)
        m_Field1 = field1
        m_Field2 = field2
    End Sub
    Public Property Field1() As String
        Get
            Return m_Field1
        End Get
        Set(ByVal value As String)
            m_Field1 = value.ToString
        End Set
    End Property
    Public Property Field2() As String
        Get
            Return m_Field2
        End Get
        Set(ByVal value As String)
            m_Field2 = value.ToString
        End Set
    End Property
End Class

You can make a list of objects:

' create a list
Dim commonList As New List(Of clsCommon)
' add some items to the list
For i As Integer = 1 to 10
   commonList.Add(New clsCommon("Item " & i, "Hello world"))
Next
' get the fifth item
Dim someCommon As clsCommon = commonList(4)
' get a property from the first item
Dim field As String = commonList(0).Field1

You can make a dictionary:

' create a dictionary with string as key
Dim commonDict As New Dictionary(Of String, clsCommon)
' add some objects
commonDict.Add("dog", New clsCommon("bark", "fetch"))
commonDict.Add("cat", New clsCommon("meow", "sleep"))
commonDict.Add("cow", New clsCommon("moo", "graze"))
commonDict.Add("ant", New clsCommon("nothing", "build"))
commonDict.Add("volvo", New clsCommon("wroom", "park"))
' access an item by key
Dim someCommon as clsCommon = commonDict("cow")
' get a property by key
Dim key As String = "cat"
Console.WriteLine("The " & key & " likes to " & commonDict(key).Field2 & ".")
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Steve SiricaSenior Web Application EngineerAuthor Commented:
Thanks GG.  I think I like the list approach.  I'm going to try a few things out and I will report back my results.  
0
Göran AnderssonCommented:
If you are going to look up things in the collection by name, I suggest the dictionary. It uses a hash table, so it's a lot faster than looking through a list for an item.
0
Steve SiricaSenior Web Application EngineerAuthor Commented:
Thanks GG.  I ended up going in the direction of the generic lists, so thanks for setting me on the correct path.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.