VB.NET – Reviewing String Collection Options

I’ll spend some time in this article reviewing some of the more common ways of handling String collections in VB.NET. Arrays in VB.NET do behave somewhat uniquely in several key aspects and working with them does take some getting used to for developers coming from a C# or Java background.

As with most things, there are several different ways of working with collections of strings in VB.NET:

  1. As a standard String array:
    Dim saTest As String(9)
  2. As a List collection:
    Dim lstTest As New List(Of String)
  3. As a StringCollection:
    Dim scTest As New StringCollection()

Further, VB.NET does need special consideration when working with MutiDimensional and jagged MultiDimensional arrays. Your MultiDimensional options are:

  1. Directly manipulating a MutiDimensional array:
    Dim saTest(1,0) As String
  2. Working with jagged MultiDimensional arrays using embedded collections (i.e.: List embedded in List, or Dictionary embedded in List):
    Dim lstTest As New List(Of Dictionary(Of String, String))

If you find this article to be useful, I have more VB.NET articles available on a range of topics that are worth a look.

Using a String Array in VB.NET

In VB.NET, arrays are zero-based and range to the number declared in the new array’s constructor. So, in the example array here:

Dim saTest As String(9)

In the example above, you can see that this String array will hold 10 array items ranging from 0 to 9.

Also worth noting is that VB.NET arrays are immutable. If you wish to change a limit that has been set on an array, then you will have to resize the array using the VB ReDim command. Furthermore, you will need to use the Preserve command to tell VB.NET to keep the data you have already entered into the array rather than starting from scratch.

Using a List Collection

The list object is reasonably simple to use. Also, it is a flexible object that can be set to types other than String in its constructor. Here is an example of setting up a new List collection and adding some items to it.

Dim lstText As New List(Of String)
lstText.Add("Test 1")
lstText.Add("Test 2")
lstText.Add("Test 3")

Once you have populated your List object, you can loop through it as follows:

For Each strListItem As String In lstText
' Do something with your strListItem object here
Next

Using a StringCollection

The StringCollection object is also fairly simple to work with, just like the List object. It is however specific to String objects, unlike List(Of X).

Dim scTest As New StringCollection()
scTest.Add("Test 1")
scTest.Add("Test 2")
scTest.Add("Test 3")

You can copy from a String array to your new StringCollection using the AddRange command:

scTest.AddRange(myArray)

Also, you can transform your StringCollection to a String array starting at a specific point in the array using the CopyTo command:

scTest.CopyTo(myArray, 0)

There are a number of ways to loop through the contents of a StringCollection. The Microsoft documentation indicates that the For Each method is the preferred way to do so:

Dim strItem As String
For Each strItem In scTest
' Do Something with your string item here
Next

MultiDimensional Arrays in VB.NET

Sometimes when working with data you will have the need to maintain collections of information. To do so, MultiDimensional arrays come in handy.

You can picture a MultiDimensional array as being like a database table with several rows and several columns. The several columns define the multidimensional part.

For example, you can directly declare a multidimensional array in VB.NET as:

Dim saTest(1,0) As String

This creates a two-dimensional array with 1 empty row. You can then add to this array as follows:

saTest(0,0) = "Id column"
saTest(1,0)="Value column"

To further expand on this example, here is how you would go about populating and resizing a  two-dimensional array inVB.NET:

Dim saTest(1,0) As String
saTest(0,0) = "Id column"
saTest(1,0)="Value column"
ReDim Preserve saTest(1,1)
saTest(0,1) = "Id column Two"
saTest(1,1)="Value column Two"

From this example you can see how we needed to use the ReDim and Preserve commands to add information to an already declared array. ReDim resizes your array while Preserve tells VB.NET to keep the data already in the array. This is really an outdated and awkward syntax (in my opinion) and is much better handled in C#. However, read on for an alternate solution that uses the Dictionary object contained in a List object in order to create a Jagged array.

Working with Jagged MultiDimensional Arrays in VB.NET

Although it is straightforward to work with jagged MultiDimensional arrays in C#, it is much more awkward  to do so using the array object in VB.NET.

In VB.NET if you want to use the standard array object, then you will need to write a loop wherein you ReDim your array as you add to it. This is not pretty and can be much better done by embedding a Dictionary object inside of a List object. Alternately you could also embed a List object inside a main List object.

Using a Dictionary and List object together (or embedded List objects) allows for simple and straightforward creation and manipulation of jagged MultiDimensional arrays. To provide and example, you can instantiate your  jagged MultiDimensional array consisting of a Dictionary object inside of a List object as follows:

Dim lstTest As New List(Of Dictionary(Of String, String))

As you can see, by using a List object with an embedded Dictionary object, you are easily able to create a Jagged MultiDimensional array in VB.NET. The best part, of course, is that you will not need to constantly ReDim your array. Here is an example of setting up a jagged MultiDimensional array, and then looping through the dimensions.

Dim lstTest As New List(Of Dictionary(Of String, String))
Dim dctTest As New Dictionary(Of String,String)
DctTest.Add("Id One", "Value One of the First Row")
DctTest.Add("Id Two", "Value Two of the Second Row ")
DctTest.Add("Id Three", "Value Three of the Third Row")
lstTest.Add( DctTest )
Dim DctTestTwo As New Dictionary(Of String, String)
DctTestTwo.Add("Id One", "Value One of the Second Row")
DctTestTwo.Add("Id Two", "Value Two of the Second Row")
lstTest.Add( DctTest Two)
For Each dctTempVal As Dictionary(Of String, String) In lstTest
For Each kvItem In dctTempVal
Dim strTmpId As String = kvItem.Key
Dim strTmpVal As String = kvItem.Value
Next
Next

From the example above you can see that this method of handling jagged arrays in VB.NET is quite flexible and intuitive, but also more verbose than declaring a Jagged array dynamically in C#. In C# you can use the native String array object to instantiate a jagged array as:

String[][] arrTest = new String[][]{new String[]{"one","two","three"}, new String[]{"r2 One","r2 Two"}};

Of course if you are so inclined, then you can also work with jagged arrays in C# using a Dictionary object embedded in a List object in a similar manner as the VB.NET example I gave above. But due to the flexible nature of arrays in C#, it is not as necessary to use this method when compared with VB.NET

About these ads

One thought on “VB.NET – Reviewing String Collection Options

  1. Pingback: VB.NET - Reviewing String Collection Options | Web App Dev | Scoop.it

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s