Click here to Skip to main content
Click here to Skip to main content

Bulilding PHP like associative array in ASP

, 4 Sep 2006
Rate this:
Please Sign up or sign in to vote.
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..

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

No Biography provided

Comments and Discussions

 
QuestionWhat about the cost? PinmemberMember 817362319-Aug-11 6:51 
Generalit mostly works... Pinmembermcm130314-Dec-06 7:15 
GeneralRe: it mostly works... [modified] PinmemberShmalex18-Jan-07 5:30 
GeneralRe: it mostly works... PinmemberGraphicNGeneer11-Nov-07 10:11 
GeneralRe: it mostly works... PinmemberGraphicNGeneer11-Nov-07 10:48 
GeneralRe: it mostly works... PinmemberSamuel Sporrenstrand12-May-08 22:22 
GeneralGood. It helps a lot folks like me. Pinmemberdde21-Nov-06 6:32 
GeneralNot bad, but... PinmemberAnonJr4-Sep-06 7:26 
GeneralRe: Not bad, but... Pinmemberrdivilbiss5-Nov-10 8:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141220.1 | Last Updated 4 Sep 2006
Article Copyright 2006 by Member 708995
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid