Using VB.Net List Or Dictionary Class And Structure To Do Multidimensional Array

Multidimensional array sometimes hard to manage. One of its cause is array index must use number i.e integer in Visual Basic. Not like Php, We cannot put string as an array index in VB.
So sometimes to code with VB is not really easy that come said from Php programmer.

In fact to make code like Array easier we can use List or Dictionary Class With Structure as its value to compensate the multidimensional Array.
List is simpler than Dictionary Class but List has it weakness. So to do more complex task we should use Dictionary.

Lets see example of List in a first place. I want to make it like a multidimensional array. I have a list of email address info that has ID, Name, Email Address itself.
Also I have a structure to put those email information in one place.

Public Structure EmailInfo
        Public ID As Integer
        Public Name As String
        Public EmailAddress As String

        Public Sub New(ByVal _id As Integer, ByVal _name As String, ByVal _email As String)
            ID = _id
            Name = _name
            EmailAddress = _email
        End Sub
    End Structure

	 Sub Main()
		Dim emlinf As EmailInfo
        Dim list_emlinf As New List(Of EmailInfo)

        emlinf.ID = 1
        emlinf.Name = "Gugi"
        emlinf.EmailAddress = "g1@exmpl.com"

        list_emlinf.Add(emlinf)

        emlinf.ID = 2
        emlinf.Name = "Tulus"
        emlinf.EmailAddress = "g2@exmpl.com"

        list_emlinf.Add(emlinf)

        For Each ei As EmailInfo In list_emlinf
            Console.WriteLine("ID {0}, Name {1}, Email Address {2}", ei.ID, ei.Name, ei.EmailAddress)
        Next

        Dim ei2 As New EmailInfo(1, "Gugi", "g1@exmpl.com")

        Dim find As Boolean = list_emlinf.Contains(ei2)
        Console.WriteLine("Bool contains {0}", find)
		Console.Readline()
	end sub()

I have a Structure to store email address info and it has more than one member to make it multidimensional. The ID member type in this structure is integer. Off course, you can set it to string type if you like.
The signature of List class is List(of Value_type). As you can see above in Sub Main() procedure the ‘List’ Class Value type can be anything from basic type i.e string, integer etc to a Structure or Class type.
We can use For Each iterator or another to list of ‘List’ Class value.

One of the ‘List’ class weakness is that when I want to find one specific email info then I have to find it with all EmailAddress structure value.
Please see block of code below:

Dim ei2 As New EmailInfo(1, "Gugi", "g1@exmpl.com")
Dim find As Boolean = list_emlinf.Contains(ei2)

I cannot find the email info with only just ID=1. I have to search it with all structure member with help of Contains(..) function.
FYI, List Class itself has an index that is integer sequence but we cannot expect that index from List is always same with respective ID.

Next, I have a Dictionary Class to overcome List weakness and make a multidimensional like array. Basically this Dictionary Class is a key value pair.
Same as above code, I have EmailAddress Structure. So here’s Sub Main() procedure:

Sub Main()
    Dim emlinf As EmailInfo
        
	Dim dict_emlinf As New Dictionary(Of Integer, EmailInfo)

	emlinf.ID = 1
	emlinf.Name = "Gugi"
	emlinf.EmailAddress = "g1@exmpl.com"

	dict_emlinf.Add(emlinf.ID, emlinf)

	emlinf.ID = 2
	emlinf.Name = "Tulus"
	emlinf.EmailAddress = "g2@exmpl.com"

	dict_emlinf.Add(emlinf.ID, emlinf)
	Dim keytofind As Integer = 1
	Dim dict_find As Boolean = dict_emlinf.ContainsKey(keytofind)

	Console.WriteLine("Dict bool contains key={0} -> {1}", keytofind, dict_find)
	Dim ei3 As EmailInfo
	ei3 = dict_emlinf(keytofind)

	Console.WriteLine("Value of contained structure ID {0}, Name {1}, Email {2}", ei3.ID, ei3.Name, ei3.EmailAddress)

	Console.ReadLine()
End Sub

Dictionary Class has a signature Dictionary(Of Key_Type, Value_Type). So I have Integer Key type and EmailInfo Value type.

I put the EmailInfo ID as a key with dict_emlinf.Add(emlinf.ID, emlinf). I can retrieve the Emailinfo member value with its ID from Dictionary key value.
Dictionary has ContainsKey(...) function to check whether specific key exist or not.

Use array like returned value syntax to return specific value from dictionary based on its key dict_emlinf(keytofind).

Summary

List and Dictionary class is useful to build up an application. My opinion is that these class is easier to manage then traditional array. I recommend to use them in web or desktop application.

If you want to retrieve a value by specific ID with any kind of key type then it is better to use Dictionary Class.

Regards,
Agung Gugiaji

Advertisements

2 responses to “Using VB.Net List Or Dictionary Class And Structure To Do Multidimensional Array

  1. Jagoan euy agung bahasa inggrisna….jadi yaaa….begittu ittuh….

  2. Vb.Net Collection samples
    http://vb.net-informations.com/collections/vb.net_collections_tutorials.htm
    Array, Dictionary , Arraylist , List …
    Winston

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