Link to home
Start Free TrialLog in
Avatar of mkosbie
mkosbie

asked on

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.
Avatar of hielo
hielo
Flag of Wallis and Futuna image

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

Response.Write( myVariable )
Avatar of mkosbie
mkosbie

ASKER

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

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

Open in new window

Avatar of mkosbie

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mkosbie

ASKER

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...
Avatar of mkosbie

ASKER

I guess this is just a limitation of VBScript.  Thanks for trying!
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
Avatar of mkosbie

ASKER

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).