Home Articles My Account Messages Tools Join
Tech Blog, ASP.Net, VB.Net, C#.Net, Programming Help, Help Guide
StellarPC.com | Creating a custom collection object (VB.Net)
Creating a custom collection object (VB.Net)
Written by: Justin Rich

Creating a custom collection object

Content: Using the class type to create custom objects

Collections are used all over in .Net programming, and there are quite a few native collections that you can utilize... however, many programmers find the need to create their own collection object because collecting your instances of objects into arrays works, but doesn't give you any additional properties. When I create new objects, I create both a Collection and a regular instance of the objec in the same .vb file (this same thing pertains to objects in C# as well, though the syntax will be different).

I start off with this:

Public Class NewObjectCollection
Inherits CollectionBase

End Class

Public Class NewObject

End Class


Go ahead and set your variables and properties for the regular object like you normally would, but hold off on that for the collection until you fully understand what the collection does.

Once you've got all the properties set up, then inside your collection object, you'll want a method to fill the collection somehow. For most of us, this is a SELECT from a database table to get all the records and we're going to be storing each as an object in the collection object. Before we create the fill method, we want to create our own Add method that shadows the CollectionBase Add method.


Public Shadows Sub Add(ByVal ThisNewObject as NewObject)
'Put custom code here
List.Add(ThisNewObject)
End Sub


Where I denoted putting custom code is a placeholder for where you want to put some specific code for each time you add a new item to the Collection. This is also a public method that you can call from your program to add items manually. We're going to be calling this from our Fill method.

In your Fill, you're going to set the function as an Integer so you're returning the number of items on a succussful fill.


Public Shadows Function Fill() As Integer
Dim rowCount as Integer = 0

Dim objNewObject as NewObject

'Set up your DB connection & reader object
'Loop on a reader object from your database selection:
While myReader.Read
objNewObject = New NewObject
With objNewObject
If Not IsDBNull(myReader("DB_FIELD")) Then .yourProperty = myReader("DB_FIELD")
'keep setting all of the properties from your database
End With

Add(objNewObject) 'adds the object to the collection
rowCount += 1 'increment the rowcount
End While

Return rowCount
End Function


So what's the point? Now, you can go back and create your properties for the collection object as you see fit. You can add a connectionstring property if you're intending to dynamically choose your database connection. You can also create different fill methods with different parameters. The collection itself works like this:


Dim objNewObjectCollection as New NewObjectCollection

objNewObjectCollection.Fill()

Dim objNewObject as NewObject

For Each objNewObject in objNewObjectCollection
'do whatever you want to with the object
Loop


With each fill method, I tend to create a second Fill method called FillDataset as a DataSet just so I've got one place to go to either fill a collection with certain parameters or fill a dataset with the same call. This helps me out a ton in my programming and hopefully it will help you out too.
New Post Next 50 | Previous 50
Post# Subject: Posted By: Time:
New Post Next 50| Previous 50