Collection Classes in VB.NET

Posted on 2004-11-25
Last Modified: 2010-04-24
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", ...................)

Question by:crestsolutions
    LVL 19

    Expert Comment

    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


    'This is the simplest example, you might add some Abstraction in your actual code
    'And not have All Public variables/collections etc.
    LVL 19

    Accepted Solution

    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

    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.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now