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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Response.Write( myVariable )
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

Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

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

Open in new window

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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...
mkosbieAuthor Commented:
I guess this is just a limitation of VBScript.  Thanks for trying!
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
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).
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP

From novice to tech pro — start learning today.