I am a veteran ASP programming, spending gigabytes of time in prepearing and creating classes using vbscript. Recentely i moved to PHP, it is a greate language. But one thing strikes my mind, why should people pick up php rather than asp. Yes, php engine is greate and efficient, but i found out the key point of php, that is associative array. The asp programmer are frequently using the code snippet like below,
Dim MyList() <BR>Redim MyList (2)<BR>MyList(0)="Sujoy"<BR>MyList(1)="Kumar"<BR>MyList(2)="Roy"
The above code is pretty simple one. I am storing the three parts of my name(First, Middle, Last in that order) . Now in my code if i want to get first name i will write as below
If i want to add some more values in the list, then when i will use them later like
address=MyList(10) it is required to remember the index number 0 or 1 or bla bla, But what if i can use the code the below way
Dim MyList<BR>MyList("fname")="Sujoy"<BR>MyList("address")="3.0 VBScript, Microsft Corp."
Yes, this is an associative array. If you know php you will see, you need to write such type of code at each 5 line.
But how could you do it in ASP. God is here, he had given us "Scripting.Dictionary" object and concept of class. You can do it folks. Lets see to do it.
What is Associative Array?
Well, even though the concept of associative array is not associated with PHP, i will go to explain that staff.
We ASP programmer all new about array, but there we need to use "index" number to access any element. Like the first sample code snippet. But in associative array, things are different. You can access any element of array not only by "index" but by "name". Like you ASP gues rmember that so much used code of "recordset" object like
Set rs=CreateObject("ADODB.RecordSet")<BR>rs.Open (...,"SELECT name FROM list")<BR>name1=rs.Fields("name").Value<BR>name2=rs.Fields(0).Value
Both name1 and name2 will give equal result. So one element can be access by index (0) or name ("name"). when you are bound to use only index, be sure you are using simple poor Array of ASP, but when you can access element by name and index both, you are defininely using Associative Array.
Request.Form is an associative array, if you check its type using "TypeName" method, it will show you "Dictionary" . "Scripting.Dictionary" is the entry point to us, the ASP programmer, to start using associative array.
Keep an eye on the page, you will get the code that will open up all the window of your imagination. But to keep up with your concentration take a look athe code snippet
Class AssocArray<BR> Private dicContainer<BR> <BR> Private Sub Class_Initialize()<BR> Set dicContainer=Server.CreateObject("Scripting.Dictionary")<BR> End Sub<BR> <BR> Private Sub Class_Terminate()<BR> Set dicContainer=Nothing <BR> End Sub
<BR> Public Default Property Get Item(sName)<BR> If Not dicContainer.Exists(sName) Then<BR> dicContainer.Add sName,New AssocArray<BR> End If<BR> <BR> If IsObject(dicContainer.Item(sName)) Then<BR> Set Item=dicContainer.Item(sName)<BR> Else<BR> Item=dicContainer.Item(sName)<BR> End If <BR> End Property<BR> <BR> Public Property Let Item(sName,vValue)<BR> If dicContainer.Exists(sName) Then<BR> If IsObject(vValue) Then<BR> Set dicContainer.Item(sName)=vValue<BR> Else<BR> dicContainer.Item(sName)=vValue<BR> End If<BR> Else<BR> dicContainer.Add sName,vValue <BR> End If<BR> End Property<BR>End Class
You can now write
Dim MyList:Set MyList=New AssocArray<BR>MyList("name")("first")="Sujoy"<BR>MyList("name")("last")="Roy"
The domain of using this associative array is endless. Suppose , yo are writing a order procesing page, and want to store the full details of transaction, which may contain 50 fields. Then you have to enter 50 fields in database table. But what if you store that data in associative array. And write a fewline of code which will convert the stored data in an xml formatted string, which can be stored! Similaraly, this data can be fetched later to use again.. Think..