Monday, January 09, 2006

Know Dot Net - DataBinding VB6 vs. VB.NET: "DataBinding VB6 vs. VB.NET
by William Ryan
Print this Article Discuss in Forums

Most business related applications, in one way or another, involve controls that were populated from a database. Even in instances where they aren't populated by a database, things have changed dramatically FOR THE BETTER in .Net. For instance, many times you may know that only x number of choices should even be in a Listbox control, and it's totally appropriate to hard code them in the IDE (a listbox full of all 50 States comes to mind). After all, if you go to the database to fill the listbox, you will incur some overhead associated with database access. On the other hand, you get flexibility, if the Puerto Rico were to become a state for instance, you wouldn't have to recompile your app. On the other hand, if you hard coded the states, you'd gain performance at the expense of some scalability. However, conisdering the likelihood and frequency of new states becomming members of the US, the tradeoff is probably worth it. Suffice to say that it really depends on the needs of your app. Nonetheless, let's look at the old VB6 way of dealing with this:

Private Sub UserForm_Initialize()
Call LoadStates(myList)
End Sub

Private Sub LoadStates(ByVal ctrl As Control)
ctrl.Clear
With ctrl
.AddItem ('Alamaba')
.AddItem ('Arkansas')
.AddItem ('Many other states')
.AddItem ('Wymoming')
End With
End Sub


What's wrong with this approach? Well, other than the Ugly Ugly Ugly weak typing (after all, I could pass in a TextBox), it takes some serious code to implement all 50 states (50 lines for the .AddItem(s) alone), if I wanted to add Items to the listbox, I have to refer to it directly, or pass the listbox to my function coupled with the new value. That's just ugly. Moreover, what if I had a bunch of other things that wanted to use the List of states? So I could modify this, "

0 Comments:

Post a Comment

<< Home