Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking if variable has been declared

Status
Not open for further replies.

multiplex77

Programmer
Dec 25, 2001
302
SG
Hi,

How can I check if a session variable (or any other variable for that matter) has been declared?

Should i use

If isNull(Session("myVar"))

or

If isEmpty(Session("myVar"))

Or is there some other better way?

Thanks for the help! :)
 
If you add 'Option Explicit' to the top of your ASP code then it will generate an error if you try to use an undeclared variable. This is also useful for debugging mispelt variable names. Tony
reddot.gif WIDTH=400 HEIGHT=2 VSPACE=3

 
Thanks very much for the tip. I am aware of Option Explicit, but I don't want it to generate a error. I need to catch the error before it gets generated.

This Session variable is set in an included page.

Thanks
 
Are you wanting to know if a variable has never been declared or if it just hasn't been used yet? It looks like you just want to know if there is any data in a session variable. Is that correct?

Are you expecting the session variable to be text or some COM object?

Thanks,

Gabe
 
I need to know if the session variable exists. If it hasn't been declared and no data has been put into it, then it won't exist. I don't want to be calling a session variable which doesn't exist.

Thanks for the help.
 
I guess what I am really trying to find out is why wouldn't you know if a session variable has been declared? You are writing the code.

Are you trying to see if specific code has been hit by the user which populates a session variable and you want to check to see what the values is?

Thanks,

Gabe
 
Calling a non-existant Session variable wont cause any errors so you could simply check to see if the Session variable had any contents or not. If it does then use it, if it doesnt then dont.
Code:
If Session(&quot;somename&quot;) <> &quot;&quot; Then
...etc
or...
Code:
If Len(Session(&quot;somename&quot;)) > 0 Then
...etc

Tony
reddot.gif WIDTH=400 HEIGHT=2 VSPACE=3

 
Thanks Tony, I guess that's what I was looking for. I thought I was getting errors from calling non-existant session variables, but I guess I was mistaken.

Thanks Gabe for your help too.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top