Browse DevX
Sign up for e-mail newsletters from DevX


VB.NET Faces Off Against Classic VB : Page 9

VB.NET, the .NET Framework, and Visual Studio.NET together form a potent combination that can simplify your code and reduce errors; however, to truly supplant existing versions of VB as a RAD tool, Visual Studio needs Break-Edit-Continue.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Populate a ListBox with values from a String array
In classic VB, you can bind controls to data from ADO Recordset objects, Data and RemoteData controls, and classes defined as data sources, but adding a simple list of values required looping through the list using the AddItem method to add each value. In addition, the syntax to create the String array in the first place is wordy. In fairness, an alternate syntax, using a Variant array is much better. In contrast, you can create a string array in VB.NET using the bind a VB.NET ListBox to an array (and notice it's a strongly typed array) by simply setting the DataSrc property. I've shown two examples in classic VB to illustrate the difference between creating a typed String array and a Variant array. The following examples each create a short String array and fill a ListBox with the values.

Classic VB (Variant array)

   Private Sub cmdPopulateList_Click()
      Dim listItems(4) As String
      Dim anItem As Variant
      listItems = Array("One", "Two", _
         "Three", "Four", "Five")
      For Each anItem In listItems
         List1.AddItem anItem
   End Sub

Classic VB (String array)

   Private Sub cmdPopulateList_Click()
      Dim listItems(4) As String
      Dim i As Integer
      listItems(0) = "One"
      listItems(1) = "Two"
      listItems(2) = "Three"
      listItems(3) = "Four"
      listItems(4) = "Five"
      For i = 0 To 4
         List1.AddItem listItems(i)
   End Sub


   Private Sub btnPopulateList_Click(ByVal sender As _
      System.Object, ByVal e As System.EventArgs) _
      Handles btnPopulateList.Click
      Dim listItems() As String = {"One", "Two", _
         "Three", "Four", "Five"}
      ListBox1.DataSource = listItems
   End Sub

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date