I\'m maintaining a Classic ASP app written in VB Script by an outside company long, long ago.
I have an array of imagefile paths, like so:
dim banner
Somewhat related is IsMissing()
to test if an optional parameter was passed, in this case an object, like this:
Sub FooBar(Optional oDoc As Object)
'if parameter is missing then simulate it
If IsMissing(oDoc) Then Dim oDoc as Object: oDoc = something
...
IsObject
could work, but IsEmpty
might be a better option - it is specifically intended to check if a variable exists or has been initialised.
To summarize:
Set
to Nothing, but will throw an error if you try it on something that isn't an objectFalse
if var
is Empty).You need to have at least dim banners
on every page.
Don't you have a head.asp
or something included on every page?
@Atømix: Replace
If Not banners Is Nothing then
and use
If IsObject(banners) Then
Your other code you can then place into an include file and use it at the top of your pages to avoid unnecessary duplication.
@Cheran S: I tested my snippets above with Option Explicit
on/off and didn't encounter errors for either version, regardless of whether Dim banners
was there or not. :-)
If a variable is declared, but not initialized, its value will be Empty
, which you can check for with the IsEmpty()
function:
Dim banners
If IsEmpty(banners) Then
Response.Write "Yes"
Else
Response.Write "No"
End If
' Should result in "Yes" being written
banners
will only be equal to Nothing
if you explicitly assign it that value with Set banners = Nothing
.
You will have problems, though, with this technique if you have Option Explicit
turned on (which is the recommendation, but isn't always the case). In that case, if banners
hasn't been Dim
ed and you try to test IsEmpty(banners)
, you will get a runtime error. If you don't have Option Explicit
on, you shouldn't have any problems.
edit: I just saw this related question and answer which might help, too.
Neither of IsEmpty, Is Object, IsNull work with the "Option Explicit" Setting, as stealthyninja above has misleadingly answered. The single way i know is to 'hack' the 'Option Explicit' with the 'On Error Resume Next' setting, as Tristan Havelick nicely does it here: Is there any way to check to see if a VBScript function is defined?