Results 1 to 31 of 31

Thread: Option explicit

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Posts
    205

    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

  2. #2
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    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.

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Option explicit

    Quote 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.

  4. #4
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    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.

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Option explicit

    Quote 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.

  6. #6
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    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.

  7. #7
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: Option explicit

    Quote 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.
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  8. #8
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    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

  9. #9
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: 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 :-)
    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.

  10. #10
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Option explicit

    Quote 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.
    Quote Originally Posted by Al42
    You should go to Tools/Options/Editor and make sure that "Require Variable Declaration" is checked.
    It is right there.

  11. #11
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    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.

  12. #12
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: Option explicit

    Quote 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."


  13. #13
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    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.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  14. #14
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    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!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  15. #15
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Option explicit

    Quote 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!!

  16. #16
    PowerPoster Keithuk's Avatar
    Join Date
    Jan 2004
    Location
    Staffordshire, England
    Posts
    2,236

    Re: Option explicit

    Quote 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.
    Quote 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.

  17. #17
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Option explicit

    Quote 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

  18. #18
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    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:
    1. Dim Num0 As Integer
    2.  
    3. Num0 = 10
    4.  
    5. MsgBox "The Number is: " & NumO
    And then you wonder why it displays "The Number is:", and not what you expect "The Number is: 10" ??

  19. #19
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    Re: Option explicit

    Quote 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.

  20. #20
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Option explicit

    Quote Originally Posted by smUX
    Wonder if that was on purpose or not? :-)
    NO, I'm just a bad speller

  21. #21
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    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.

  22. #22
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Option explicit

    Quote 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" )

  23. #23
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    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.

  24. #24
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Option explicit

    Use DefType statements to avoid using Variants. This works well with a naming convention strategy.

    VB Code:
    1. DefInt I  ' variables starting with I are Integers
    2. DefLng L 'variables starting with L are Longs
    3. DefStr S 'variables starting with S are Strings
    4. 'all others are Variants.
    5.  
    6. 'will cause a TypeMismatch error.
    7. lngTemp = "Hello"
    8.  
    9. 'will cause an Overflow error
    10. intTemp = 100000

  25. #25
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    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.

  26. #26
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: Option explicit

    Quote 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 ..

  27. #27
    Member
    Join Date
    Mar 2006
    Posts
    39

    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.

  28. #28
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Option explicit

    Quote 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

  29. #29
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Option explicit

    Quote 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.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  30. #30
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: Option explicit

    To summarized. ALWAYS USE OPTION EXPLICIT! LOL
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  31. #31
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Option explicit

    Quote 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
  •  



Click Here to Expand Forum to Full Width