Option explicit
I have always wrote this, but all I know of it is that it forces you to create variables. Is that all it does? I mean I am tempted not to use it so keep my code shorter (without dimming all the variables) - Thanks in advance
Printable View
Option explicit
I have always wrote this, but all I know of it is that it forces you to create variables. Is that all it does? I mean I am tempted not to use it so keep my code shorter (without dimming all the variables) - Thanks in advance
Option Explicit requires you to define all your variables explicity. If you do not define your variables explicitly (i.e. As String, As Integer) then they will be defined as Variants - slowing down operations and increasing memory use.
Also, any variables used but not declared will be created and defined as variants - thus increasing the chance of hard-to-debug errors through typos. - these typos would be caught with Option Explicit.
There's never a reason not to use it.
Do not do that. Always predeclare your variables. You could be opening yourself up to a potential nightmare of debugging problems.Quote:
Originally Posted by Cr250
Experience is the best, although sometimes harshest, teacher.
So, if you want to not use Option Explicit, then remove it.
I'll bet, however, that in very short order, you would find yourself putting it back. :)
I personally don't use it...but I'm not a professional programmer...pretty much all professional programmers use it...what's that say about my programming technique (or lack thereof :-))
Basically, Option Explicit is one hell of a good bug finder. If you mis-spell a variable, it will tell you because you haven't declared it...that's sometimes a 2 hour job trying to find bugs like that :-)
I don't care if you are a professional nose picker...if you are writing a program, you should ALWAYS use Option Explicit.Quote:
Originally Posted by smUX
One day I'll get into the habit of using it, but I pretty much taught myself everything I know about programming (up to a point, of course)...and with me as a teacher, I'm doomed :-)
Yes you are. :D ;)Quote:
Originally Posted by smUX
I had to debug a 10,000 line code someone else wrote without any Option Explicit, and found atleast 15 errors caused just by misspelled variable names.
ALWAYS USE OPTION EXPLICIT. :)
You should go to Tools/Options/Editor and make sure that "Require Variable Declaration" is checked. Then your code will always start with Option Explicit, and you won't have to remember to add it. The first time you rewrite your code so much that there's no hope of ever getting it back to the point that it'll work, only to finally find out that your initial error was a typo in a variable name, you'll kick yourself around the block for not having turned that option on.
Somewhere there is an option, I have it turned on but can't find it right now, you can set so new forms have it automatically inserted. It might be coming from my MZTools. I set it so long ago I can't find it.Quote:
Originally Posted by smUX
:blush:
Quote:
Originally Posted by TysonLPrice
It is right there.Quote:
Originally Posted by Al42
Yeah, someone already mentioned. It's not that I don't want to add it, it's that I don't usually do projects large enough to require it. My largest program was less than 50k of source with about 3 or 4 forms and it was for personal, not widespread, use so I don't bother too much with ensuring it works.
If I did something that I planned on giving out to many people I'd probably use it and I can adapt quite easily to coding with it...but it's just a burden to me when I'm only coding as a messabout or for myself :-)
Very true.Quote:
Originally Posted by Hack
Actually in BASIC and initial versions of Visual Basic too, there was no option to force a variable declaration. At that time though, the convention of variable declaration was to declare short variable names (like A,B,C, i, j, m etc.). But in today's scenerio where the convention is to use long variable names and chances of typing mistakes is high, this keywork "Option Explicit" was introduced.
So choose either method - Short variable names or Option Explicit.
The history goes back a bit futher than just MS and just VB...
Initially variable names could only be one character and the datatype of the variable was indicated by a suffix character.
In mainframe BASIC's I've used:
A% - integer
A$ - string
A - floating point
(that was it for datatypes!).
DIM was only a for arrays.
DIM X%(99)
Then variable names increased to more than one character - those were happy days!
Then they allowed us to DIM and DECLARE our variables and indicate a datatype!
ps... The fact that VB6 does not allow an OPTION STRICT is absurd in my opinion - at least .Net got that right!
A-freaking-Men Brother szlamany!!Quote:
Originally Posted by szlamany
That will only work on new projects. If you made project previous to this option you will have to type Option Explicit on the top of every code window.Quote:
Originally Posted by Al42
It doesn't matter how big or small you project it, its just good programming practice in VB. :thumb:Quote:
Originally Posted by smUX
The number of apps I've downloaded from www.planet-source-code.com that don't use it. As previously been said it can sometimes take hours to debug.
Then we invented programs to go into the source code and chop variable names down to one character (or two if we had more than 26 variables) and make as many multiple statements per line as possible, so we could shoehorn a program into available memory (which, in some environments, was less than 16k).Quote:
Originally Posted by szlamany
When I reply to threads in the vbforums, I always put Option Explicit in the code that I post to get people to:
1) Notice Option Explicit, for the people who don't know about it
2) To show that the code I post is without errors (well, sintax errors anyways)
I think VB should not alow you to take Option Explicit off, you can have error so easily...
The most common mistake I used to make:
And then you wonder why it displays "The Number is:", and not what you expect "The Number is: 10" ??VB Code:
Dim Num0 As Integer Num0 = 10 MsgBox "The Number is: " & NumO
Wonder if that was on purpose or not? :-)Quote:
Originally Posted by CVMichael
NO, I'm just a bad speller :DQuote:
Originally Posted by smUX
Bad spellers need Option Explicit...not me :-P
It's OK, if you have not learned your lesson now, you still got time.Quote:
Originally Posted by smUX
You will learn to to Option Explicit once you spend a few hours to find a syntax error. (I opened MS Word to find out the spelling for "syntax" :D)
www.dictionary.com is quicker (I used it earlier to confirm "syntactically" was right, someone corrected someone else :-))
And I've had errors in the past that have been attributed to not using Option Explicit...however, the time taken to FIND those error(s) was far less than the time it would have taken faffing around with Option Explicit and delcaring every single variable I want to use...I'm a variable freak and I use them for everything :-)
Use DefType statements to avoid using Variants. This works well with a naming convention strategy.
VB Code:
DefInt I ' variables starting with I are Integers DefLng L 'variables starting with L are Longs DefStr S 'variables starting with S are Strings 'all others are Variants. 'will cause a TypeMismatch error. lngTemp = "Hello" 'will cause an Overflow error intTemp = 100000
I used to use "DEFINT A-Z" in QBasic...and I always used that when starting a program...dunno why, just always did :-)
Quote:
Originally Posted by smUX
LOL .. we dont need to know how to spell, just how to reuse the correct variable ..
Can't you still use the suffixes %, &, !, #, and $ without using DIM?
Just curious. BTW, I always use Option Explicit. I didn't know there was an option to automatically include it in your code. I'll have to try it. :)
Sure, it'll keep Num0& from being a variant. But it won't let you find the value of Num0& by typing NumO&.Quote:
Originally Posted by kberry79
Yes - and when I hop onto the mainframe to write a down-and-dirty one-time-use program to fix some data issue - I sometimes use those variables without declaration.Quote:
Originally Posted by kberry79
But I also go into mainframe mode in general:
Actually putting the % on the 1 and 100 literals will make them integers. That's important for memory (I still remember the 16K days and having to CHAIN to the next executable to process some more logic!) - and also important for formula evaluation.Code:For X% = 1% to 100%
.
.
.
Next X%
To summarized. ALWAYS USE OPTION EXPLICIT! LOL :D
That is as simple as it gets - enough of this debate - common guys... :rolleyes:Quote:
Originally Posted by [B
Usage is not mandated but it is best if you do - what else does anyone needs to know.