UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
 
   Reply to this topicStart new topic
> Variable Declaration In Vbscript    
 
   
nmurray
post Mar 25 2015, 05:20 AM
Post#1



Posts: 439
Joined: 28-February 11
From: UK


Hi,
I've been getting into VBScript recently and am pretty happy with some of the things it has helped me achieve (automatic updating of databases from text files that are generated by another system for instance).

I understand that all variables in VBScript are variants, so what's the point of declaring them at all? The best argument I've found on the web is that it helps prevent errors. How? I'd like to see a properly constructed argument in favour of the declaration of variables to help me make up my mind as to wether it's just a waste of time or not.
Go to the top of the page
 
pere_de_chipstic...
post Mar 25 2015, 05:49 AM
Post#2


UtterAccess Editor
Posts: 10,110
Joined: 8-November 07
From: South coast, England


Hi

From an Access point of view:

If you do not declare a variable then say you assign a value to one variable and then use that variable in a later part of the code but misspell the variable name then your code will not detect the misspelling.

If you treat all variables as global then it is possible to miss assigning a value to the variable in a part of your code that then uses the variable whose value, assigned in some other part of the code, is no longer valid.

hth
Go to the top of the page
 
cheekybuddha
post Mar 25 2015, 05:55 AM
Post#3


UtterAccess VIP
Posts: 9,275
Joined: 6-December 03
From: Telegraph Hill


Hi Neil,

Coding in VBScript is a bit like coding in VBA without the Option Explicit declaration, and also that you can't type your variables.

There is no requirement to declare you variables, as you point out. However, declaring them does maintain a sense of reference when you revisit your code, especially if it's complex - it's good to know what you're playing with.

Having said that, you need to work out what's best for you (and potentially for anyone else who may have to maintain your code at a later date).

hth,

d
Go to the top of the page
 
nmurray
post Mar 25 2015, 06:16 AM
Post#4



Posts: 439
Joined: 28-February 11
From: UK


I take you're point about Option Explicit.
I've added that to one of my VBSript files and found I missed the declaration of a couple of variables. I've added them to the Dim statements and it no longer errors... But it didn't before I forced declaration anyway, which was my point. I can see how it would save a little time debugging spelling errors in future and will bear that in mind.
Go to the top of the page
 
cheekybuddha
post Mar 25 2015, 06:20 AM
Post#5


UtterAccess VIP
Posts: 9,275
Joined: 6-December 03
From: Telegraph Hill


Ha ha! I had forgotten that you could use Option Explicit in vbScript! (Haven't used it for a long time!)

thumbup.gif

d
Go to the top of the page
 
cheekybuddha
post Mar 25 2015, 06:26 AM
Post#6


UtterAccess VIP
Posts: 9,275
Joined: 6-December 03
From: Telegraph Hill


I suppose that you can turn it the other way around.

Why do you declare your variables in Access VBA?

Remove Option Explicit and you needn't bother - you can code just like vbScript.

dontknow.gif

d
Go to the top of the page
 
nuclear_nick
post Mar 25 2015, 07:00 AM
Post#7



Posts: 1,391
Joined: 5-February 06
From: Ohio, USA


QUOTE
But it didn't before I forced declaration anyway, which was my point.

QUOTE
I'd like to see a properly constructed argument in favour of the declaration of variables to help me make up my mind as to wether it's just a waste of time or not.


It's not just spelling errors. It's use of a variable. If you do misspell a variable that is not explicitly declared, you will get results. You may even think everything is fine.

Then the building collapses. Why? Because someone trusted your 'x+y=z' function, when it should have been 'x+a=z'.

My point is that, yes, perhaps your function didn't error before you added the 'Explicit' option. How long did you trust the results before finding out they may have been wrong?

So perhaps you aren't building actual buildings. But incorrect calculations because you used the 'wrong' variable affects everything that calculation was used for.

(Okay, maybe not the best constructed... It's early, I still haven't had my coffee.)
Go to the top of the page
 
BruceM
post Mar 25 2015, 07:41 AM
Post#8


UtterAccess VIP
Posts: 7,022
Joined: 24-May 10
From: Downeast Maine


Here on the Maine coast I am one time zone ahead of Nick, and I have had my coffee, so I will agree with his point and add some further detail. Or maybe this was his point. It can be difficult to discern the true meaning of pre-coffee statements. laugh.gif

If you do not require variable declaration, then a typo could be treated as a variable, which will either be null or nothing (not quite sure how this works in VBScript) since it has never been assigned a value. Where MyValue is declared and assigned a value, it can be used in a calculation. MyValeu in the same calculation may produce a result, but not the one you expect. Otherwise it could produce an error that could be quite difficult to locate.

I can think of no reason not to require variable declaration and declare all variables if I have that choice.
Go to the top of the page
 
nmurray
post Mar 25 2015, 08:18 AM
Post#9



Posts: 439
Joined: 28-February 11
From: UK


I can see the advantage of declaring variables in tandem with Option Explicit, but from the majority of script files i've seen out there, it isn't used that often, although people do actually use the Dim statement.

I'll change my argument, as I can see the point of declaration in tandem with Option Explcit now. What's the point of declaring variables in a weakly typed environment if you don't use Option Explicit? This does seem to be the norm rather than the exeption. Here's a random bit of code I've just googled, which is indicative to the general standard out there.

CODE
   Dim objShell
   Dim strPath

   Set objShell = Wscript.CreateObject("Wscript.Shell")
   strPath = objShell.SpecialFolders("Desktop")
   wscript.echo strPath


This dosen't stop typos, as Option Explicit is not used, does nothing to help debugging and saves no memory as the variables are variants.
Go to the top of the page
 
nuclear_nick
post Mar 25 2015, 08:35 AM
Post#10



Posts: 1,391
Joined: 5-February 06
From: Ohio, USA


I would say that is a 'too each their own' kind of thing, and there are a few unknown factors, like what kind of development environment was the script created in.

Going back over my scripts, I see where I've done the same thing as you've found, not knowing that you could 'Option Explicit' in vbs. (I plan on correcting that in what I have, though.)

QUOTE
This dosen't stop typos, as Option Explicit is not used, does nothing to help debugging and saves no memory as the variables are variants.


I don't know about the memory part... does the script suddenly take up more memory when it comes upon an implicitly declared variable? Does that make it execute more slowly than if the variable were declared? Are most scripts too small to really bench test this theory?

So I could say... go do what you want. Just be prepared for some "real programmer" to give you a "what-for" when coming across your implicit coding.
Go to the top of the page
 


Custom Search
RSSSearch   Top   Lo-Fi    10th December 2017 - 09:25 PM