By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,109 Members | 969 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,109 IT Pros & Developers. It's quick & easy.

Check if variable exists.

100+
P: 101
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.
Jan 22 '08 #1
Share this Question
Share on Google+
3 Replies


kadghar
Expert 100+
P: 1,295
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:
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. Dim n As Integer
  3. Sub One()
  4.     Dim i As Integer
  5.     For i = 1 To 10: i = i: Next
  6. End Sub
  7. Sub Two()
  8.     For n = 1 To 10: n = n: Next
  9. End Sub
  10. Sub Test()
  11.     Dim t1 As Single: Dim t2 As Single
  12.     Dim j As Long
  13.     t1 = Timer
  14.     For j = 1 To 10000000: Call One: Next
  15.     t1 = Timer - t1
  16.     t2 = Timer
  17.     For j = 1 To 10000000: Call Two: Next
  18.     t2 = Timer - t2
  19.     MsgBox t1 & Chr(13) & t2
  20. End Sub
^.^ 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
Jan 22 '08 #2

Expert 5K+
P: 8,434
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.
Jan 23 '08 #3

Expert 5K+
P: 8,434
...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.
Jan 23 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.