Creating Custom Collections and Binding Them

I thought this was too cool so I decided to post an example on it. 

First Create A Class
We need to create a class the items of which we will add to our custom collection.  This one’s pretty simple, it’s a car:

‘  Quick class to give us some properties

Public Class CarItem

   Public _Make As String

   Public _Model As String

 

   ‘you have to implement the properties to be able to bind it later on

   ‘ (i.e. declaring the variables as Public won’t work)

   Public Property Make() As String

      Get

         Return _Make

      End Get

      Set(ByVal Value As String)

         _Make = Value

      End Set

   End Property

 

   Public Property Model() As String

      Get

         Return _Model

      End Get

      Set(ByVal Value As String)

         _Model = Value

      End Set

   End Property

End Class

 

Create The Custom Collection
The important parts to note are the first line where we inherit from System.Collections.CollectionBase.  This provides all the basic functionality of the collection to us.

The next thing we do is to re-implement the Add and Item properties.  This is what makes it so cool.  Later on when you access these properties the intellisense knows that your adding a “CarItem” object and you have all the properties of the “CarItem” available.

The Microsoft article made the Item property ReadOnly.  I’m not sure why, I decided to implement the Item property just like the Base object’s Item Property (allowing Get and Set).

 

‘This is the custom collection that we can populate and do things like bind to .NET controls

Public Class Cars

   Inherits System.Collections.CollectionBase

 

 

   ‘Re-implement the Add method, but type it to our object

   Public Sub Add(ByVal Car As CarItem)

      list.Add(Car)

   End Sub

 

   ‘Reimplement the Item property but type it

   Public Property Item(ByVal index As Integer) As CarItem

      Get

         Return CType(list.Item(index), CarItem)

      End Get

      Set(ByVal Value As CarItem)

         list.Item(index) = Value

      End Set

   End Property

End Class

 

Using The New Collection
Now that we’ve created our new collection we can use it.  The code below shows an example of how you might bind the collection to a datagrid.  Obviously you’d probably be adding the data from a database and not manually, but this is good enough for example.  The last line also shows how you can use the item property to inspect the items within the collection.

Dim MyCarCollection As New Cars

 

‘This would be better done through a database  🙂

Dim Car1 As New CarItem

With Car1

   ._Make = “Ford”

   ._Model = “Ranger”

End With

MyCarCollection.Add(Car1)

 

Dim Car2 As New CarItem

With Car2

   ._Make = “Jeep”

   ._Model = “Wrangler”

End With

MyCarCollection.Add(Car2)

 

‘Assign our custom car collection to the cars datagrid and bind it

dgCars.DataSource = MyCarCollection

dgCars.DataBind()

 

Response.Write(MyCarCollection.Item(0).Make)

Here’s an Example of The DataGrid After Being Bound

Datagrid after it's bound to a custom collection

Pretty cool eh?

Here’s the original Microsoft Article which I basically re-implemented here 😛

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vaconCreatingYourOwnCollectionClass.asp

DSC_3446


 

DSC_3446

Originally uploaded by firstdivision.

Found a cool place to take pictures of planes coming into TF Green Airport in Rhode Island. It was getting dark when I got there so this shot was taken at 1600 ISO, that’s why it’s so grainy. I’ll go back someday a little earlier to get some better shots (hopefully).

Upgrade to WordPress 2.0

Hey there everyone. Just thought I’d tell everyone that I’ve upgraded http://blog.killfly.com to WordPress 2.0. On yourside I suppose it doesn’t look a whole lot different, but there’s all kinds of new things for me to play with….that’s the real reason I had to write this post…

For those technical people out there, here’s what I did:

Backup Current Site

  • Exported database from MyPhpAdmin as SQL file in case I needed to restore.
  • Downloaded all the files from the blog.killfly.com subdomain to my local hard drive.

Get New Files And Prepare Upload

  • Downloaded and extracted the new WordPress2 ZIP file to my desktop.
  • Copied the config.php and php.ini from my local backup to my downloaded copy (so I can upload one big folder).

Do Actual Upload and Update

  • Deleted all the wordpress files from the blog.killfly.com server (made sure to leave my stats folders).
  • Upload new files.
  • Ran the /wp-admin/upgrade.php file as per the instructions.
  • Opened http://blog.killfly.com/ in my browser.

I realzed my custom banner was missing (my fault)…

Fix Missing Banner

  • Uploladed my personalHeader.jpg file from my local backup at wp-content\themes\default\images
  • Uncommented the line in header.php using theme editor to re-display the custom header.

Done!

Granted, my blog is fairly small, and I didn’t have any uploaded images. But I was very happy at how easy the upgrade was.

-Andrew