Click here to Skip to main content
15,891,951 members
Articles / Programming Languages / PHP
Article

Bulilding PHP like associative array in ASP

Rate me:
Please Sign up or sign in to vote.
2.55/5 (5 votes)
4 Sep 20063 min read 63.2K   15   9
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() <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

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

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

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


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionWhat about the cost? Pin
Member 817362319-Aug-11 5:51
Member 817362319-Aug-11 5:51 
Generalit mostly works... Pin
mcm130314-Dec-06 6:15
mcm130314-Dec-06 6:15 
GeneralRe: it mostly works... [modified] Pin
Shmalex18-Jan-07 4:30
Shmalex18-Jan-07 4:30 
GeneralRe: it mostly works... Pin
GraphicNGeneer11-Nov-07 9:11
GraphicNGeneer11-Nov-07 9:11 
GeneralRe: it mostly works... Pin
GraphicNGeneer11-Nov-07 9:48
GraphicNGeneer11-Nov-07 9:48 
GeneralRe: it mostly works... Pin
Samuel Sporrenstrand12-May-08 21:22
Samuel Sporrenstrand12-May-08 21:22 
GeneralGood. It helps a lot folks like me. Pin
dde21-Nov-06 5:32
dde21-Nov-06 5:32 
GeneralNot bad, but... Pin
AnonJr4-Sep-06 6:26
AnonJr4-Sep-06 6:26 
GeneralRe: Not bad, but... Pin
rdivilbiss5-Nov-10 7:51
rdivilbiss5-Nov-10 7:51 

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

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