When I first installed VB.NET I was filled with a sense of anticipation and excitement. This was the Next Big Thing in the VB world.

There are two ways to approach a new piece of software, you dive right in or you read the manual. I believe it is physically impossible for a member of the human species to follow the second course. Especially is this the case with VB.NET. The manual came on two CDs, zipped. A total of almost 2 gig. I am already an old man – I would be dead before I finished reading it and it would take seven eternities before I understood it.

So I loaded it up and started programming. The first thing I wanted to do was put some data in a list box. Pretty simple, eh? In every version of VB which I have used, that is from 3 to 6 I could do this in my sleep. My fingers remember how to type this code. And whenever Microsoft changed its data access methods we had to learn a few things, but it was still pretty straight forward. Until now.

How do you get data in .NET? Well you can use a dataset. Or a data table. Sometimes you want a dataview. You might also want to read up on data adapters and datareaders. And so you do. Read up on them, that is.

So now you’ve got your data. And you want to put it into your list box. In the good old days you would add an item to your list box, but not only that you could also add an Itemdata property to each item. This was very useful when, for example, you wanted to add a name to a list box, but be able to access the record ID for that name. You would add the ID to the Itemdata property. VB has always had this. Everyone used it. A list box or a combo box was practically unusable without it.

Naturally, I tried to add an Itemdata property to my list box in .NET. Guess what? It no longer supports Itemdata. What were they thinking? There is a way around it. If you bind the list box to a data source, such as a dataset then there is a display property and a value property. You can set the value property to hold the same data which the Itemdata property held in earlier versions.

But what if you don’t want to use a dataset, and go through all the hassle of dataadapters. As the manual says, datasets are just one way of accessing data and it is not always the most appropriate method to use.

Now I don’t use datasets too often. I, usually, prefer to access the database directly, grab my data, and populate the list box. The reason is mainly because of the size and type of application that I usually write.

I read, somewhere, that there is a trick you can use to put, in this case, the customer ID into the list box. But as I read it, and tried it, I found out that it isn’t a trick at all. It is intrinsic to the way that VB.NET, and the other .NET languages work. A list box, or a combo box, don’t really hold values, they hold objects. So to put some data into a list box you can add, for example, the customer’s name. The name is a string which is an object, and the default display value of the list box is the string value of the object, in this case the name. But if you write a class and create an object you can add the object to the list box and have it display whatever you want, and at the same time the list box will also contain whatever other properties are contained within the object.

For example, suppose you wish to display your customers in the list box in the format surname, firstname – a fairly common way to display names. And you also want to be able to find the customer ID for that name. You would create a class, we will call it clsCustomer with a constructor Private Sub New(ByVal CustomerID as Long, ByVal Surname as String, ByVal Firstname as String) Within that constructor you would assign the CustomerID, Surname and Firstname to fields or properties you have defined within the class. By creating a property of CustomerID the VB.NET IDE uses Intellisense to expose the CustomerID property when you use the class. This makes programming easier, faster, and less prone to error.

Finally you need to override the ToString method. The list box needs to know what to display and you have added an object to it. So you override the ToString method to display what you want, in this case Surname, Firstname. And there you have it. In fact, you can add whatever properties you want to this class, and they are all contained within the list box item. If you had a property PhoneNo, for example you can access the phone number from the list box like this PhoneNo = Ctype(ListBox.SelectedItem,clsCustomer).PhoneNo

The Ctype operator casts the List Box item into a type of Customer object and then you grab the property PhoneNo.

VB.NET took away some goodies, but in most cases they replaced them with better goodies. You just have to find them.