Make sure a variable exists / Include guard

Let me just start by saying that I despise ASP Classic / VB Script syntax more than anything ever created.  Honestly, I'd rather program in Whitespace (http://en.wikipedia.org/wiki/Whitespace_(programming_language)).

Now onto the problem.  I have an include file that I want to include across multiple pages.  It uses a variable from another include file that the user MAY OR MAY NOT have already included.  One, both, or neither of the includes can show up on any of my sites pages, so I can't just move the variable between files (unfortunately).

In a decent language, I could use something like a header guard to prevent compilation errors, but VB Script has no such capacity.  I don't even really care if the file is included directly, just that this one particular variable exists.  I thought about something like this in the include:

If Not myVariable Then
     Dim myVariable = "This is my variable"
End If

But because of scope, that myVariable declaration won't be seen by the rest of the page.  That kind of defeats the purpose.

In simple terms my task is: Check if a variable is already defined.  If it is, do nothing.  If it isn't, define (Dim) it.
LVL 8
mkosbieAsked:
Who is Participating?
 
hieloCommented:
>> This results in code that has the actual Dim statement appear twice on the page (which is what causes the error).
Yes. The only way I can think of is to omit the "Dim myvariable" completely:


If VarType(myVariable) = 0 Then
	myVariable = 5
End If
Response.Write myvariable
 
There is no "Exists" function. There is an "Exists" method for dictionary objects:
http://www.devguru.com/Technologies/vbscript/quickref/vbscript_list.html
 
but the issue is that you will still need to create the dictionary object at some point, which brings you back to square one.

Open in new window

0
 
hieloCommented:
If 0 = vartype(myVariable) Then
      Dim myVariable
      myVariable=5
End If

Response.Write( myVariable )
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
mkosbieAuthor Commented:
Hi hielo.  I still get a Name Redefined (800a0411) error if I try this code.
Dim myVariable
If VarType(myVariable) = 0 Then
	Dim myVariable
	myVariable = 5
End If

Open in new window

0
 
hieloCommented:
try:
Dim myVariable
If VarType(myVariable) = 0 Then
	myVariable = 5
End If
Response.Write myvariable

Open in new window

0
 
mkosbieAuthor Commented:
Yes, that works, but its not my goal.  Refer back to the original question.  I have an include file (IncludeFile1.asp) I'm using in an ASP page (Page.asp).  I need to make sure it's included in another include file (IncludeFile2.asp) which is also included in Page.asp.  This results in code that has the actual Dim statement appear twice on the page (which is what causes the error).

The particular page I'm working on (Page.asp) needs both IncludeFile1.asp and IncludeFile2.asp.  Other pages may need only IncludeFile1.asp, so moving the variables/references to other files won't help.
0
 
mkosbieAuthor Commented:
Yes, I'm aware I can drop the Dim, but that's bad form.  What about Const and Class declarations?  I can't drop those...
0
 
mkosbieAuthor Commented:
I guess this is just a limitation of VBScript.  Thanks for trying!
0
 
hieloCommented:
Indeed a limitation. I researched this some more but could not find any way to achieve what you are trying to do.

On another note, in case you do not know this already, you do NOT have to use vbscript in classic ASP. You can use JScript/Javascript. Of course you would have to convert the other include files to JScript, but at least in jscript you would be able to do what you are after:
if( "undefined" == typeof(myvariable) )
{
...
}

Thanks for the points and best of luck.
Hielo
0
 
mkosbieAuthor Commented:
I know I can use JScript.  My boss won't let me.  =P.  (Actually, if I were allowed to program this the way I wanted, it would be in .NET with C# and none of this would matter).
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.