Monthly Archives: June 2013

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.
Continue reading