[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1156
  • Last Modified:

If syntax for undefined variables

I'd like to write an If Then on an undefined variable.  The page that's calling is an include.  It's defined on aspx pages that are calling the include, but not the asp pages that are also calling the include.  So, I'd like to write it as ...

If myVar <> '' Then

Unfortanetly, I get the variable is undefined from the asp pages.  I don't want to dim it on every asp page.  Also, can I say something like... If myVar is undefined Then dim myVar  etc..
0
sl1nger
Asked:
sl1nger
  • 7
  • 4
  • 2
  • +1
1 Solution
 
Ashish PatelCommented:
Try IsObject(var) function
0
 
Ashish PatelCommented:
But the most stupid stuff in ASP is, if you even define the variable in Any block of your program it will consider it as Declared variable
0
 
b0lsc0ttCommented:
Using isObject() will look at the type of a defined variable.  It won't help if you want to see if the variable is defined.

Your options to "test" this might be limited, especially if you are requiring variables to be declared (e.g. Option Explicit).  This is probably the case here or you wouldn't get an error.  One option I can think of is use something else to test if the page is aspx or asp.  One way would be Request.ServerVariables("SCRIPT_NAME").  You could even just look at the last letters for the extension.

Let me know how this helps or if you have a question.

bol
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
sl1ngerAuthor Commented:
How would I right to get the last letters?  basically last 4.  So if aspx then whatever..
0
 
b0lsc0ttCommented:
Since the letters could be asp or aspx I suggest you use a split.  The code could be something like ...

arExt = Split(Request.ServerVariables("SCRIPT_NAME"), ".")
strExt = arExt(1)
If Lcase(strExt) = "asp" then

Let me know if you have a question or need more info.

bol
0
 
b0lsc0ttCommented:
You could easily test for aspx instead.  If you use the split then remember to not include the period in the If.

You could also use Instr() and just look for .aspx or .asp in the Request variable.

bol
0
 
sl1ngerAuthor Commented:
b0lsc0tt - how would you use the instr syntax in the include file??

 <%if Instr(Request.ServerVariables("SCRIPT_NAME")) = ".aspx" Then  %>  hurray <% End If%>
0
 
sl1ngerAuthor Commented:
Well I used that example in the page and the conditional works, but when I put it into the include it fails.

my_page.aspx
-- my_include.asp
I guess it's trying to read the extension of the include and not the parent page...  any other suggestions>??
0
 
b0lsc0ttCommented:
An Instr example would be ...

If Instr(Request.ServerVariables("SCRIPT_NAME"), ".aspx") <> 0 then
    ' it is there
End if

If the string isn't found then 0 is returned so the If above will be true if it is .aspx.  The value would not be 0 but the location of the . in .aspx (starting with 1).

There is some good info on instr() at http://www.w3schools.com/vbscript/func_instr.asp and that site also mentions the Split() function.

Let me know if you have any other questions.

bol
0
 
b0lsc0ttCommented:
It won't try to read the extension of the include file.  Using include puts the script in the main page BEFORE the script is run.  The result should be the main page's file name, whether the script is in the main page or in the include.

Do you need the include file name?  I have assumed it was the main file name you needed.  Please confirm which you need.

If it is the main page then that is what you should be getting.  If it failed then there is another reason.  Please elaborate on the code you used and the pages (and their relationship to each other).

bol
0
 
sl1ngerAuthor Commented:
Maybe it's how I have it written within the include calling another include..

<%  Dim arExt, strExt
        arExt = Split(Request.ServerVariables("SCRIPT_NAME"), ".")
        strExt = arExt(1)
        If LCase(strExt) = "asp" Then
%>
<!--#include virtual="/includes/apps.asp"-->
<%End If %>
0
 
b0lsc0ttCommented:
Please elaborate.  What are the pages and what is the relationship between them??

Even an include inside an include will be added to the main page before script is run.  The result of the line above should be the same whether it is in the main page, the first include or even in the include inside the first include.

What type of page is it?  My expertise is more in ASP and not .NET.  A .net page might be an exception.

You might try Response.Write(ing) the ServerVariable to see what the result is.  The problem might be something else.

bol
0
 
b0lsc0ttCommented:
hongjun,

Thanks for taking the time to clean and your help here.  I recommend you accept my comment at http:#20028226 .  Let me know if there is a question about this.  I hope it helps.

bol
0
 
Computer101Commented:
Forced accept.

Computer101
EE Admin
0

Featured Post

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.

  • 7
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now