Check if variable exists.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zensunni
    New Member
    • May 2007
    • 101

    Check if variable exists.

    In a perfect world, I wouldn't have to do this, but I'm at the mercy of event scripting for a program.

    there's an event that runs every so often that triggers an subroutine. I'd like to declare a variable in that subroutine, but not have to worry about it when an event triggers it again. If I just go:

    Public variable As String

    .. it will give an error. I need to check if the variable exists... can that actually be done? I've yet to see a method to check if a variable exists.

    plz help. Thanks.
  • kadghar
    Recognized Expert Top Contributor
    • Apr 2007
    • 1302

    #2
    Originally posted by zensunni
    In a perfect world, I wouldn't have to do this, but I'm at the mercy of event scripting for a program.

    there's an event that runs every so often that triggers an subroutine. I'd like to declare a variable in that subroutine, but not have to worry about it when an event triggers it again. If I just go:

    Public variable As String

    .. it will give an error. I need to check if the variable exists... can that actually be done? I've yet to see a method to check if a variable exists.

    plz help. Thanks.
    Dim variable as string

    is not a executable line. it doesnt matter if you declare it one million times, it wont have any effect (only that each time the variable will be reset) if you want it not to reset, define it outside the Sub.

    Well, actually it's going to make your code a little slower, It's going to take 1 more second to run for each 10 million times (aprox) you declare a variable. Here, check this out:
    [CODE=vb]
    Option Explicit
    Dim n As Integer
    Sub One()
    Dim i As Integer
    For i = 1 To 10: i = i: Next
    End Sub
    Sub Two()
    For n = 1 To 10: n = n: Next
    End Sub
    Sub Test()
    Dim t1 As Single: Dim t2 As Single
    Dim j As Long
    t1 = Timer
    For j = 1 To 10000000: Call One: Next
    t1 = Timer - t1
    t2 = Timer
    For j = 1 To 10000000: Call Two: Next
    t2 = Timer - t2
    MsgBox t1 & Chr(13) & t2
    End Sub[/CODE]

    ^.^ It's somekind of fun experiment.

    anyway, im not even sure that you can check if a variable exists if it doesnt exist yet. i.e. you cannot say IF variable something THEN declare variable. Since it didnt exist at the begining of the IF (or if you're not using option explicit, in that very moment it'll become a variant (object in vb2005))

    HTH

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      Not sure I fully understand the requirements. But perhaps a static variable is what you're looking for. A static variable defined in a Sub or Function (in VB6 at least - not 100% certain about script) doesn't get thrown away when you leave the routine and recreated on next entry. It stays around and keeps its value.

      Note that this can have serious consequences, especially for reentrant or recursive routines.
      Last edited by Killer42; Jan 23 '08, 02:54 AM.

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Originally posted by kadghar
        ...Since it didnt exist at the begining of the IF (or if you're not using option explicit, in that very moment it'll become a variant (object in vb2005))
        Oh, you had me going for a moment then. I was about to upbraid you being a naughty programmer and driving with your Option Explicit disengaged. Then I remembered this is VB Script, not "real" VB.

        Comment

        Working...