www.webdeveloper.com
Results 1 to 5 of 5

Thread: Proper way to use Variables

  1. #1
    Join Date
    Jul 2010
    Posts
    18

    Red face Proper way to use Variables

    Good morning,

    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.

    Thanks,

    Adelyra

  2. #2
    Join Date
    Dec 2008
    Posts
    488
    "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.

    Code:
    Dim myObject, myString
    Set myObject = Server.CreateObject("MSWC.NextLink")
    myString = "This is how you define a literal."
    You should always declare your variables. It helps with scope for starters, but it's also good habit. In PHP or JavaScript, you should know that if you declare a variable in the beginning of a script, but then again with the same variable name in the beginning of a function, the variable remains unchanged outside the function. So it helps you to maintain order among variables. ASP VBScript is a little lenient on you in this regard, as is the JavaScript engine in browsers, but that's not the point.

    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.

  3. #3
    Join Date
    Dec 2008
    Posts
    488
    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.

  4. #4
    Join Date
    Jul 2010
    Posts
    18
    Thank you so much for your replies! These helped a lot and I now have a better understanding of how VBScript variables work.

  5. #5
    Join Date
    Dec 2008
    Posts
    488
    Any time!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles