How to tell whether a variable in ASP has been declared
Asked Answered
E

4

7

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?

Edmondedmonda answered 4/11, 2010 at 15:46 Comment(3)
Did either of the two answers below work for you? :-)Brade
I decided to go with a different approach, where this was no longer an issue.Edmondedmonda
Well, you could perhaps decide which answer you liked best and Accept it (even if you went with a different approach) just for the sake of rewarding at least one of those who took his or her time and effort to try to help you out. Just sayin'.Rubyeruch
R
11

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

Rubyeruch answered 4/11, 2010 at 17:25 Comment(2)
I originally had it tagged ASP.NET, which was retagged by DOK as ASP-classic. As for Option Explicit, the shopping cart software being used on this site doesn't set it, and being a live site, I really don't want to make any change that could cause lots of errors, even if it's in the interest of ASP best practices. I think I'm going about it wrong. Instead of Dim'ing a variable and then <!-- #include file="header.asp" -->, I should instead perform a check inside header.asp to determine which file it's being included in.Edmondedmonda
If you don't have Option Explicit turned on, you can also use IsEmpty to check for undeclared variables. You won't be able to tell the difference between undeclared and uninitialized, though.Hild
B
5

@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
Brade answered 4/11, 2010 at 16:51 Comment(1)
Is it possible to call a function inside header.asp if I'm including it using a SSI: <!-- #include file="header.asp" --> ?Edmondedmonda
F
3

What about:

 If NOT IsEmpty(myvariable) Then...

that seems to have been working for me.

Filamentous answered 15/5, 2013 at 13:48 Comment(0)
O
0

I use the VarType function to detect if the variable is defined. If the tested variable is not defined, VarType returns the value vbError (10). Interesting to note that the optional parameter cannot be the last parameter else ASP tosses an error.

function Sample(p1,p2,p3,p4)
    if VarType(p3) <> vbError then
        'do something with p3
    end if
end function

ThisWorks=Sample(1,2,,3)
ThisFails=Sample(1,2,3,)
Oneupmanship answered 8/12, 2021 at 17:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.