?
Solved

Creating a dynamic class of classes

Posted on 2008-01-31
5
Medium Priority
?
359 Views
Last Modified: 2010-04-21
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.  
0
Comment
Question by:Steve Sirica
  • 3
  • 2
5 Comments
 
LVL 4

Author Comment

by:Steve Sirica
ID: 20788139
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
 
LVL 29

Accepted Solution

by:
Göran Andersson earned 2000 total points
ID: 20789028
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
 
LVL 4

Author Comment

by:Steve Sirica
ID: 20789277
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
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 20790909
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
 
LVL 4

Author Closing Comment

by:Steve Sirica
ID: 31426752
Thanks GG.  I ended up going in the direction of the generic lists, so thanks for setting me on the correct path.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Free Data Recovery software is an advanced solution from Kernel Tools to recover data and files such as documents, emails, database, media and pictures, etc. It supports recovery from physical & logical drive after a hard disk crash, accidental/inte…
Suggested Courses
Course of the Month5 days, 5 hours left to enroll

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question