473,385 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Check if variable exists.

101 100+
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
3 4268
kadghar
1,295 Expert 1GB
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
Killer42
8,435 Expert 8TB
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
Killer42
8,435 Expert 8TB
...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

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

Similar topics

0
by: Thomas Scheffler | last post by:
Hi, I have several xslt documents that import each other in a specific way to allow resulting documents to be layout in a common way. In the "master stylesheet" I need some kind of test to check...
2
by: Mike | last post by:
I´ve got a number of SPAN elements named "mySpan1", "mySpan2", "mySpan3" etc, and want to set their "style.display" to "inline". This works (only needs to work on IE5.5+): for (var x = 1; x <...
3
by: opt_inf_env | last post by:
Hello, In some examples of a PHP implementation I saw the following: if ( isset(x) and x==12) I do not understand what for before to check whether some variable has some value one need to check...
5
by: jez123456 | last post by:
Hi, I’ve written a c# program to compact certain msaccess databases. The way this works is to compact say C:\test1.mdb to C:\temp.mdb, then delete C:\test1.mdb and rename C:\temp.mdb as...
5
by: Shapper | last post by:
Hello, I am using this code line to check i a variable exists: If cookie Is Nothing Then .... How can I check if cookie is empty when cookie exists? Thanks, Miguel
2
by: shapper | last post by:
Hello, How can I check if a parameter, for example "name", is available in a QueryString? I need to create a default value if the parameter is not available. Thanks, Miguel
2
by: CCLeasing | last post by:
If (Directory.Exists(@"c:\indigo\" + txtOfficial + @"\" + comOffice.Text + @"\" + numTerm.Value.ToString())) { storereport(); } else { Directory.CreateDirectory(@"c:\indigo\" + txtOfficial +...
3
by: Yansky | last post by:
If I have the following code: var abc; if(!abc){ alert('test'); }
1
by: soulfly73 | last post by:
hello All, I have an question, I'm very newwhen it comes to ASP.net. I am building an site where I want to check on the existince of an image in an certain directory. No I have some questions...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.