|
-
Jun 9th, 2006, 10:52 AM
#1
Thread Starter
Addicted Member
Option explicit
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
If you can’t help, dont change the subject with useless questions about the problem
-
Jun 9th, 2006, 10:56 AM
#2
Re: Option explicit
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.
-
Jun 9th, 2006, 10:57 AM
#3
Re: Option explicit
 Originally Posted by Cr250
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
Do not do that. Always predeclare your variables. You could be opening yourself up to a potential nightmare of debugging problems.
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.
-
Jun 9th, 2006, 10:57 AM
#4
PowerPoster
Re: Option explicit
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 :-)
Well, everyone else has been doing it :-)
Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
Expect more to come in future
If I have helped you, RATE ME! :-)
I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!
And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.
-
Jun 9th, 2006, 10:59 AM
#5
Re: Option explicit
 Originally Posted by smUX
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 :-))
I don't care if you are a professional nose picker...if you are writing a program, you should ALWAYS use Option Explicit.
-
Jun 9th, 2006, 11:03 AM
#6
PowerPoster
Re: Option explicit
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 :-)
Well, everyone else has been doing it :-)
Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
Expect more to come in future
If I have helped you, RATE ME! :-)
I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!
And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.
-
Jun 9th, 2006, 11:07 AM
#7
Re: Option explicit
 Originally Posted by smUX
...and with me as a teacher, I'm doomed :-)
Yes you are. 
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.
Last edited by iPrank; Jun 9th, 2006 at 11:12 AM.
-
Jun 9th, 2006, 11:11 AM
#8
Re: 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.
The most difficult part of developing a program is understanding the problem.
The second most difficult part is deciding how you're going to solve the problem.
Actually writing the program (translating your solution into some computer language) is the easiest part.
Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.
Please Help Us To Save Ana
-
Jun 9th, 2006, 11:12 AM
#9
Re: Option explicit
 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 :-)
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.
-
Jun 9th, 2006, 11:14 AM
#10
Re: Option explicit
 Originally Posted by TysonLPrice
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.

 Originally Posted by Al42
You should go to Tools/Options/Editor and make sure that "Require Variable Declaration" is checked.
It is right there.
-
Jun 9th, 2006, 11:15 AM
#11
PowerPoster
Re: Option explicit
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 :-)
Well, everyone else has been doing it :-)
Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
Expect more to come in future
If I have helped you, RATE ME! :-)
I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!
And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.
-
Jun 9th, 2006, 12:36 PM
#12
Re: Option explicit
 Originally Posted by Hack
I don't care if you are a professional nose picker...if you are writing a program, you should ALWAYS use Option Explicit.
Very true.
Regards,
Mark
Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."
-
Jun 9th, 2006, 12:53 PM
#13
Re: Option explicit
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.
-
Jun 9th, 2006, 12:58 PM
#14
Re: 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!
-
Jun 9th, 2006, 01:02 PM
#15
Re: Option explicit
 Originally Posted by szlamany
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!!
-
Jun 9th, 2006, 02:29 PM
#16
Re: Option explicit
 Originally Posted by Al42
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.
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.
 Originally Posted by smUX
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.
It doesn't matter how big or small you project it, its just good programming practice in VB.
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.
Keith
I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.
-
Jun 9th, 2006, 02:48 PM
#17
Re: Option explicit
 Originally Posted by szlamany
Then variable names increased to more than one character - those were happy days!
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).
The most difficult part of developing a program is understanding the problem.
The second most difficult part is deciding how you're going to solve the problem.
Actually writing the program (translating your solution into some computer language) is the easiest part.
Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.
Please Help Us To Save Ana
-
Jun 9th, 2006, 03:02 PM
#18
Re: Option explicit
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:
VB Code:
Dim Num0 As Integer
Num0 = 10
MsgBox "The Number is: " & NumO
And then you wonder why it displays "The Number is:", and not what you expect "The Number is: 10" ??
-
Jun 9th, 2006, 03:14 PM
#19
PowerPoster
Re: Option explicit
 Originally Posted by CVMichael
2) To show that the code I post is without errors (well, sintax errors anyways)
Wonder if that was on purpose or not? :-)
Well, everyone else has been doing it :-)
Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
Expect more to come in future
If I have helped you, RATE ME! :-)
I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!
And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.
-
Jun 9th, 2006, 03:16 PM
#20
Re: Option explicit
 Originally Posted by smUX
Wonder if that was on purpose or not? :-)
NO, I'm just a bad speller
-
Jun 9th, 2006, 03:17 PM
#21
PowerPoster
Re: Option explicit
Bad spellers need Option Explicit...not me :-P
Well, everyone else has been doing it :-)
Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
Expect more to come in future
If I have helped you, RATE ME! :-)
I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!
And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.
-
Jun 9th, 2006, 03:26 PM
#22
Re: Option explicit
 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.
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" )
-
Jun 9th, 2006, 03:30 PM
#23
PowerPoster
Re: Option explicit
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 :-)
Well, everyone else has been doing it :-)
Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
Expect more to come in future
If I have helped you, RATE ME! :-)
I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!
And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.
-
Jun 9th, 2006, 03:41 PM
#24
Re: Option explicit
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
-
Jun 9th, 2006, 03:42 PM
#25
PowerPoster
Re: Option explicit
I used to use "DEFINT A-Z" in QBasic...and I always used that when starting a program...dunno why, just always did :-)
Well, everyone else has been doing it :-)
Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
Expect more to come in future
If I have helped you, RATE ME! :-)
I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!
And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.
-
Jun 9th, 2006, 04:03 PM
#26
PowerPoster
Re: Option explicit
 Originally Posted by smUX
Bad spellers need Option Explicit...not me :-P
LOL .. we dont need to know how to spell, just how to reuse the correct variable ..
-
Jun 9th, 2006, 04:26 PM
#27
Member
Re: Option explicit
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.
-
Jun 9th, 2006, 04:37 PM
#28
Re: Option explicit
 Originally Posted by kberry79
Can't you still use the suffixes %, &, !, #, and $ without using DIM?
Sure, it'll keep Num0& from being a variant. But it won't let you find the value of Num0& by typing NumO&.
The most difficult part of developing a program is understanding the problem.
The second most difficult part is deciding how you're going to solve the problem.
Actually writing the program (translating your solution into some computer language) is the easiest part.
Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.
Please Help Us To Save Ana
-
Jun 9th, 2006, 08:40 PM
#29
Re: Option explicit
 Originally Posted by kberry79
Can't you still use the suffixes %, &, !, #, and $ without using DIM?
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.
But I also go into mainframe mode in general:
Code:
For X% = 1% to 100%
.
.
.
Next X%
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.
-
Jun 9th, 2006, 09:10 PM
#30
PowerPoster
Re: Option explicit
To summarized. ALWAYS USE OPTION EXPLICIT! LOL
-
Jun 9th, 2006, 09:33 PM
#31
Re: Option explicit
 Originally Posted by [B
Simply [/B]Me]To summarized. ALWAYS USE OPTION EXPLICIT! LOL 
That is as simple as it gets - enough of this debate - common guys...
Usage is not mandated but it is best if you do - what else does anyone needs to know.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|