Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell whether a variable in ASP has been declared

Let me start off by saying I'm a PHP developer, not an ASP one. (And I really wish ASP had isset().) And I'm working in a live environment so I don't really have an opportunity to do any testing.

All the resources I've found suggest different ways to test for the existence of a variable.

Here's what I'm trying to do:

On SOME pages, I set a variable which holds a value for a robots <meta> tag:

dim dsep_robots
dsep_robots = "nofollow,noindex"

All pages include header.asp. In my header file, I want to test whether dsep_robots has a value and if so, output that value, otherwise, output nothing.

I think that testing whether dsep_robots has a value might look like this:

if not dsep_robots = "" then
    '...
end if

Best practices in PHP state that when you're using a variable that may or may not exist, you should always test if (isset($var)) {...} (not doing so will trigger a Notice if the variable doesn't exist).

Is there such a thing in ASP -- i.e. do I really need to test if it exists, or can I simply test if it has a value?

like image 547
WNRosenberg Avatar asked Nov 04 '10 15:11

WNRosenberg


3 Answers

ust by the way, your question isn't about classic ASP, it is a VBScript question. VBScript can occur in scripts outside of ASP. And compilation isn't done in VBScript, because it is an interpreted language. Nevermind.

I think there's some confusion here -- and your question seems to have more to do with uninitialized variables than undeclared variables. For undeclared variables, see below.

For uninitialized, try the function IsEmpty. For checking for null, try the function IsNull.

dim x
x = 1
dim t
Response.write isempty(x)
Response.write "<br>"
Response.write isempty(t)   

Will display:

False

True

Detecting Undeclared Variables

If you include Option Explicit in your header, use of a non-declared variable will cause an runtime error. If your script is not Option Explicit it will not generate an error, and there is no function that will tell you if the variable has been declared or not. This sounds sloppy, and it is, but it was intentional.

The only way you can escape this is to actually set Option Explicit and then trap the error that you will get when you try to use the undeclared variable. If you trap this particular error you will find that it has Err.Number = 500. So, the following will do what you want:

Option Explicit

dim x

On Error Resume Next

Response.Write dsep_robots  
If Err.Number > 0 Then
    Response.Write Err.Number
end if

Of course, if you set Option Explicit and your code is rife with undeclared variables then you'll get errors being thrown all over the place, so you'd need to set On Error Resume Next at the top of your code so you can successfully ignore it, and only trap it when you want to.

By the way, here's Microsoft's online reference for VBScript:

http://msdn.microsoft.com/en-us/library/d1wf56tt(v=VS.85).aspx

like image 132
Cyberherbalist Avatar answered Oct 02 '22 13:10

Cyberherbalist


@Jazzerus: I'd recommend putting the code within header.asp into a Sub, something like

Sub outputHeader(ByRef MyTitle, Byref dsep_robots)    
  'contents of header.asp
End Sub

...and then in your calling pages include header.asp right at the top and use

outputHeader "Title for this page", "value you want dsep_robots to have for page"

If you don't set dsep_robots on that page, then just leave the second parameter blank ("")

Then just checking if the variable is empty or not within the Sub should suffice:

If dsep_robots <> "" Then
  Response.Write dsep_robots
End If
like image 37
stealthyninja Avatar answered Oct 01 '22 13:10

stealthyninja


What about:

 If NOT IsEmpty(myvariable) Then...

that seems to have been working for me.

like image 22
Jeremythuff Avatar answered Oct 03 '22 13:10

Jeremythuff