Wednesday 30 November 2011

NEW WAY TO HANDLE VARIABLES

The EASIEST way to deal with variables!!

Allow me to introduce myself.

My name is Rob Collyer, I lay eyes upon ASP years ago and have been heavily coding since.
It always struck me as a complete pain in the neck the way in which we have all been dealing with variables in ASP, for as long as we have used ASP.

What do I mean? ........ Allow me to explain with this example of the hard way:-

<% FirstName = Request.Form("FirstName") Surname = Request.Form("Surname") Address1 = Request.Form("Address1") Address2 = Request.Fomr(Address2") ... ... %>

Rather than go on longer than necessary into yet more lines looking exactly the same as the ones above, to put my point across, I'll put you out of your misery.

Why do variables have to be this TEDIOUS to deal with??? I wanted a way to read in all variables at once in one go, with just one function call.... I must be mad.... sure enough, I was told this as well as things like 'It's not possible', etc.

Sure enough, at the time, it wasn't possible. To my rescue came Microsoft, with Version 5 of the VBScript engine and a new command called Execute. Execute(string) basically executes a string as though it were ASP.. So Execute("Response.write ""Hello"") .... would have the same effect as just using response.write "hello"

As soon as I saw this new feature I knew instantly what I could do with it:-

<% For Each Field in Request.Form TheString = Field & "= Request.Form(""" & Field & """)" Execute(TheString) Next %>

We are setting variables automatically. That one little tiny piece of code, has saved me so much time over the last year, you will not believe.
Ok, it does have it flaws, Forms with image submit buttons pass form variable names like "submit.X", and "submit.Y" (Co-ordinates where you clicked on the submit image) which obviously wont do for variable names in ASP, and results in the white error page from hell.... But we can start to prevent these things:-

<% For Each Field in Request.Form tmpField = Replace(Field,".","") 'remove full stops TheString = tmpField & "= Request.Form(""" & Field & """)" Execute(TheString) Next %>

Why stop there?? There are many silly characters that are fine in HTML controls as names, but when setting variable names automatically like this in VBScript, these characters will not do. You can easily add additional lines as above to escape other characters like spaces, hyphens and all manner of other ones too.

You've got multiple controls on forms.... how are you gonna assign variables to multiple select boxes?
As a developer, I found it handy to read these 'multiple' part form variables into an array... I will build upon the above code to handle multiple items and create arrays from them:-

<% For Each Field in Request.Form tmpField = Replace(Field,".","") 'remove full stops ItemCount=Request.Form(Field).count IF ItemCount > 1 then
execute "redim " & field & "(" & itemcount -1 & ")" 'Dynamically dimension the array
For Item = 0 To ItemCount - 1
TheString = tmpField & "("&Item&")= request.form(""" & field & """).item(" & Item + 1& ")"
Next
Else
TheString = tmpField & "= Request.Form(""" & Field & """)"
End If
Execute(TheString)
Next
%>

I hope by now you can see the potential of what all of the above means to you as the developer.....
I urge you all to write your own functions that'll handle variables the way you want them to, and please remember.... FORMS were just an example, don't stop there, after all there are Querystrings, Cookies, Session variables, Application Variables, ServerVariables. Then there are recordsets (why not?) and the dictionary objects, etc, etc, etc.

Just remember, you are now armed with info which will save you a great deal of time, why not get busy writing some variable handling functions of your own??

If anybody wants a function to selectively handle Form, Querystring, Cookies, Session, Application and Server variables then I have one...just mail me. It also has a parameter to run in DEBUG mode which will show you variables in all objects, their values and whether their names present a problem during conversion. It'll also flag those controls that can be set to arrays, etc.

Anyway, those that do feel inspired to get writing their own handlers, let me ask one thing.... Send me a copy of your finished function(s), this is pretty new stuff and I'm well open to being inspired by YOUR code.

Feel free to contact me with your feedback.

Enjoy!
http://www.aspwebpro.com/tutorials/asp/newwaytohandlevar.asp

No comments:

Post a Comment