[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Collection Classes in VB.NET

Posted on 2004-11-25
2
Medium Priority
?
338 Views
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", ...................)


0
Comment
Question by:crestsolutions
  • 2
2 Comments
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 12673971
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
 
LVL 19

Accepted Solution

by:
arif_eqbal earned 1500 total points
ID: 12674027
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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

834 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