Collection Classes in VB.NET

I want a collection class to be a property of another class in VB.NET. How do I implement this? What I am asking is analagous to a recordsets and fields in VB6. I want to be able to create an instance of one class via another class. e.g.

dim x as new recordset
dim y as new field

set y = x.fields.add("Field1", ...................)


crestsolutionsAsked:
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.

arif_eqbalCommented:
Create a class like....


Public Class Class1
    'Normal properties
    Public MyProperty As String
   
    'a Collection variable
    Public MyCollection As Collection = New Collection

    Public Sub MyMethod()
       'Normal methods
    End Sub

End Class


Then you can use the class as

Dim objClass1 as Class1=New Class1

objClass1.MyCollection.Add("Item","Key")

'This is the simplest example, you might add some Abstraction in your actual code
'And not have All Public variables/collections etc.
0
arif_eqbalCommented:
I am sorry I did'nt read your question properly

you also need to return the Collection object so that it can be stored back like

dim y as new field
y=objClass1.MyCollection.Add("Item","Key")

Now in this case if you are storing a String in the collection MyCollection then naturally you'd need y to be of the type String

Similarly you can have any other class say Class2 that will be stored in the Collection and the object y will be of the type Class2

But, the Add method of the Collection does not return anything, so you need to write your own Add that will return either a specific Class like Class2 or a generic Object which you can typecast to y.

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
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
Visual Basic.NET

From novice to tech pro — start learning today.