65.9K
CodeProject is changing. Read more.
Home

Bulilding PHP like associative array in ASP

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.55/5 (5 votes)

Sep 4, 2006

3 min read

viewsIcon

63793

Bulilding PHP like associative array in ASP

Introduction

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()
Redim MyList (2)
MyList(0)="Sujoy"
MyList(1)="Kumar"
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

fname=MyList(0)

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
MyList("fname")="Sujoy"
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")
rs.Open (...,"SELECT name FROM list")
name1=rs.Fields("name").Value
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.

My 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
  Private dicContainer
  
  Private Sub Class_Initialize()
   Set dicContainer=Server.CreateObject("Scripting.Dictionary")
  End Sub
  
  Private Sub Class_Terminate()
   Set dicContainer=Nothing   
  End Sub


  Public Default Property Get Item(sName)
   If Not dicContainer.Exists(sName) Then
    dicContainer.Add sName,New AssocArray
   End If
   
   If IsObject(dicContainer.Item(sName)) Then
    Set Item=dicContainer.Item(sName)
   Else
    Item=dicContainer.Item(sName)
   End If   
  End Property
  
  Public  Property Let Item(sName,vValue)
   If dicContainer.Exists(sName) Then
    If IsObject(vValue) Then
     Set dicContainer.Item(sName)=vValue
    Else
     dicContainer.Item(sName)=vValue
    End If
   Else
    dicContainer.Add sName,vValue    
   End If
  End Property
End Class

You can now write

Dim MyList:Set MyList=New AssocArray
MyList("name")("first")="Sujoy"
MyList("name")("last")="Roy"

Happy coding

Few Thought

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