Proper way to use Variables
I'm somewhat new to ASP however I have a background in PHP. The use of variables between the 2 langauges is quite different and I'm wondering a few things:
- Dim vs. Set: What's the difference and when should I use them?
- Is it bad to just define a variable without declaring it?
- What variables should be cleared or set to nothing and when?
I know these may be fairly simple questions but I feel as though I need to start from the beginning and really get a handle on variables before I write any more ASP applications.
I have researched google however I found it difficult searching for these answers. There didn't seem to be a lot of information specific to these questions.
"Set" in VBScript is like saying "New". It takes an undefined variable and creates an object based on the class used in the statement. You still need to declare the object's reference in the head or function, depending on scope.
Dim myObject, myString
Set myObject = Server.CreateObject("MSWC.NextLink")
myString = "This is how you define a literal."
The reason a variable is set to "Nothing" is for memory cleanup. It's another good habit, but matters less on short scripts than it does on long running actual programs that use more RAM. It can also come in handy if you need to reset an object as part of a conditional statement. But if you never clean your objects out of memory and you have a script that runs heavily for long enough, it will build up all those objects and your RAM will be over used.
As for ASP, once the VBScript is finished running, all the variables, objects, etc used by the script are dumped immediately, so this isn't as important. But again, it's good habit for later.
Last edited by jamesbcox1980; 11-08-2010 at 08:12 AM.
BTW, it might help your google searches if you search for "VBScript" instead of ASP. ASP stands for "Active Server Pages" and can use several different languages. Classic ASP uses VBScript (Visual Basic Script).
If you buy a book on VBScript, it will help with learning the language. There aren't any books out there on just "ASP" that will help you.
Thank you so much for your replies! These helped a lot and I now have a better understanding of how VBScript variables work.
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Tags for this Thread