|
From: Tony Bansten on 27 Jun 2008 14:33 Occasionally I saw VBS script where local variables were declared on top with a statement like DIM myvar However most of my own vbs script run without such declaration. When do I need them and when not? Tony
From: Bob Barrows [MVP] on 27 Jun 2008 14:45 Tony Bansten wrote: > Occasionally I saw VBS script where local variables were declared on > top with a statement like > > DIM myvar > > However most of my own vbs script run without such declaration. > When do I need them and when not? > In script? Never. It's just good practice to use them in conjunction with "option explicit" to help prevent hard-to-debug errors resulting from mistyping variable names -- Microsoft MVP - ASP/ASP.NET Please reply to the newsgroup. This email account is my spam trap so I don't check it very often. If you must reply off-line, then remove the "NO SPAM"
From: Bob Barrows [MVP] on 27 Jun 2008 14:53 Tony Bansten wrote: > Occasionally I saw VBS script where local variables were declared on > top with a statement like > > DIM myvar > > However most of my own vbs script run without such declaration. > When do I need them and when not? > I should probably elaborate on that. Without "option explicit" you could do somthing like this: myvar = 33 '50 lines of code later" msgbox myvr leading to many minutes or hours of scratching your head over the failure of the msgbox to display 33 With "option explicit": option explicit dim myvar myvar = 33 '50 lines of code later" msgbox myvr When you run this you will get an immediate error saying something like "undeclared variable 'myvr'" making you go huh? Where did I use "myvr"? A quick search reveals the problem and you are offf and running. Simple good programming practice. -- Microsoft MVP - ASP/ASP.NET Please reply to the newsgroup. This email account is my spam trap so I don't check it very often. If you must reply off-line, then remove the "NO SPAM"
From: Michel Posseth [MCP] on 27 Jun 2008 15:47 VB / VBS / VBA has the posibility to use explicit variabels and implicit , implicit is not the recomended way It leads to code that is prone to errors and more difficult to debug. I would recomend you to use Option Explicit at the start of every code file Any variable that is not explicitly declared will then generate an error this forces you to do it the "right" way and probably saves you hours of debugging . So in short use Dim :-) HTH Michel "Tony Bansten" <tonytony(a)hotmail.com> schreef in bericht news:48653284$0$6553$9b4e6d93(a)newsspool3.arcor-online.net... > Occasionally I saw VBS script where local variables were declared on top > with a statement like > > DIM myvar > > However most of my own vbs script run without such declaration. > When do I need them and when not? > > Tony >
From: Tom Lavedas on 27 Jun 2008 15:56 On Jun 27, 2:45 pm, "Bob Barrows [MVP]" <reb01...(a)NOyahoo.SPAMcom> wrote: > Tony Bansten wrote: > > Occasionally I saw VBS script where local variables were declared on > > top with a statement like > > > DIM myvar > > > However most of my own vbs script run without such declaration. > > When do I need them and when not? > > In script? Never. It's just good practice to use them in conjunction with > "option explicit" to help prevent hard-to-debug errors resulting from > mistyping variable names > > -- > Microsoft MVP - ASP/ASP.NET > Please reply to the newsgroup. This email account is my spam trap so I > don't check it very often. If you must reply off-line, then remove the > "NO SPAM" Bob, Shouldn't that be "In script? Sometimes never."? ;;^)) Fixed and Dynamic arrays need to be DIMed, don't they? Especially, if they're to have multiple dimensions. Also, another good practice is to DIM local variables within Functions and Subroutines to keep from variable conflicts between routines. In fact, that is almost a necessity, if your application grows past 40 or 50 lines - and you want to keep your sanity ;-) I have chased my tail for hours on more than a few occasions because I didn't DIM a variable and used the same name in the main and a SUB or two subs. The two subs are supposed to be independent, but end up interacting through that common name (acting as a global, when that wasn't the intention - ugh!). I suspect that there are other cases, too. That's all that come to mind, right now. Tom Lavedas =========== http://members.cox.net/tglbatch/wsh/
|
Next
|
Last
Pages: 1 2 Prev: Modifying HKCU or HKU subkey that has been restricted Next: Help with Logon script error |