Check if an Object exists in VBScript

前端 未结 6 1429
我在风中等你
我在风中等你 2020-12-16 10:59

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         


        
相关标签:
6条回答
  • 2020-12-16 11:39

    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
    
    ...
    
    0 讨论(0)
  • 2020-12-16 11:45

    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:

    • IsEmpty(var) will test if a variable exists (without Object Explicit), or is initialised
    • IsNull(var) will test if a variable has been assigned to Null
    • var Is Nothing will test if a variable has been Set to Nothing, but will throw an error if you try it on something that isn't an object
    • IsObject(var) will test if a variable is an object (and will apparently still return False if var is Empty).
    0 讨论(0)
  • 2020-12-16 11:45

    You need to have at least dim banners on every page.

    Don't you have a head.asp or something included on every page?

    0 讨论(0)
  • 2020-12-16 11:47

    @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. :-)

    0 讨论(0)
  • 2020-12-16 12:02

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

    0 讨论(0)
  • 2020-12-16 12:02

    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?

    0 讨论(0)
提交回复
热议问题