?
Solved

Make sure a variable exists / Include guard

Posted on 2008-11-11
10
Medium Priority
?
591 Views
Last Modified: 2012-05-05
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.
0
Comment
Question by:mkosbie
  • 5
  • 5
10 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 22935461
If 0 = vartype(myVariable) Then
      Dim myVariable
      myVariable=5
End If

Response.Write( myVariable )
0
 
LVL 82

Expert Comment

by:hielo
ID: 22935463
0
 
LVL 8

Author Comment

by:mkosbie
ID: 22935596
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 82

Expert Comment

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

Open in new window

0
 
LVL 8

Author Comment

by:mkosbie
ID: 22935734
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
 
LVL 82

Accepted Solution

by:
hielo earned 2000 total points
ID: 22935890
>> 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
 
LVL 8

Author Comment

by:mkosbie
ID: 22936042
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
 
LVL 8

Author Closing Comment

by:mkosbie
ID: 31515753
I guess this is just a limitation of VBScript.  Thanks for trying!
0
 
LVL 82

Expert Comment

by:hielo
ID: 22946787
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
 
LVL 8

Author Comment

by:mkosbie
ID: 22946808
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question