.Net, ASP.NET, Collections, Programming, VB.NET, Web Development

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.

Continue reading “VB.NET – Reviewing String Collection Options”

Advertisement
.Net, ASP.NET, Collections, Programming, VB.NET, WebForms

The ASP.NET Dictionary Object

I have been working with ASP.NET collections and thought a more detailed look into one of the most useful classes also know as the Dictionary collection object is in order.

To review: the Dictionary class is part of the System.Collections.Generic Namespace. If you haven’t already, then it’s worth having a look at the list of collections contained in the namespace to become familiar with the various collections options available to you. The main benefit of the using collections from the Generic namespace is that the collections are strongly typed, giving you better performance and type safety.

The Dictionary collection stores key – value pairs much like a Hashtable from the System.Collections Namespace.  However where a Hashtable has an inbuilt re-ordering system based on your provided keys, the Dictionary collection works on a First-In-First-Out (FIFO) basis. This can be a real boon if you want key value pairs that don’t get randomly re-arranged. The Dictionary object is not documented to be FIFO, but it has been working this way since .NET 1.1 so I hope that this will continue to be the case. Similar objects to the Dictionary class are the SortedList and the SortedDictionary classes, but these do have inbuilt sorting similar to the Hashtable’s sorting, so they are only situationally useful.

In addition to the Dictionary object, I like to use ArrayLists and string arrayx. So as part of my example code I show how to store a  String array inside a Dictionary object as the value. This can be quite useful in a number of ways depending on your goals, but the flexibility this method offers is something to keep in mind.

If you are interested in reading the API for the Dictionary object then I definitely recommend reading the official Microsoft Documentation. An interesting point that’s covered by the  documentation is that using TryGetValue is the most efficient syntax to use rather than ContainsKey or ContainsValue.

Continue reading “The ASP.NET Dictionary Object”