Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > what is Option Explicit


Reply
 
Thread Tools Display Modes
  #1  
Old 04-12-2005, 06:20 PM
larrylaffer133 larrylaffer133 is offline
Freshman
 
Join Date: Feb 2005
Posts: 30
Default what is Option Explicit


what is Option Explicit? i see this code in the general declarations section of a form sometimes and i dont know what it does or why people add it to their code.
Reply With Quote
  #2  
Old 04-12-2005, 06:25 PM
reboot's Avatar
reboot reboot is offline
Keeper of foo

Retired Moderator
* Guru *
 
Join Date: Nov 2001
Location: Graceland
Posts: 15,612
Default

Option Explicit forces you to declare your variables.
__________________
~ Quod non mortiferum, fortiorem me facit ~

Avatar by lebb
Reply With Quote
  #3  
Old 04-12-2005, 06:25 PM
mrjeffy321 mrjeffy321 is offline
Ultimate Contributor
 
Join Date: Apr 2003
Location: Texas, USA
Posts: 1,623
Default

Option Explicit tell the compiler that you are required to delcare all your variables before you use them. otherwise, without Option Explicit, you can start using variables names whatever you want, and VB will automatically create one for you. This sounds great, how nice of VB, but really, this is a bad idea, it leads to errors in your code (because if you miss type a variable name, VB creates a new one without telling you, storing its value and continues running, un-be-knownst to you, the variable you think you were using is being left alone.
Also, if dont declare a variable yourself, vb will auto declare it as a variant (which take the most memory). a variant can hold any data type, numbers letters, ...., but that wont dee you much good when you want to multiply a variable you think is containing a number and is acutally containing a combobox (although that would be a rather poor coding job to make that misstake, it could theroetically happen).

It is just a good idea to use Option Explicit always.
Reply With Quote
  #4  
Old 04-12-2005, 06:26 PM
larrylaffer133 larrylaffer133 is offline
Freshman
 
Join Date: Feb 2005
Posts: 30
Default

dont you always need to declare variables though?
Reply With Quote
  #5  
Old 04-12-2005, 06:28 PM
reboot's Avatar
reboot reboot is offline
Keeper of foo

Retired Moderator
* Guru *
 
Join Date: Nov 2001
Location: Graceland
Posts: 15,612
Default

You should. But Vb doesn't force you to, unless you force it to force you.
__________________
~ Quod non mortiferum, fortiorem me facit ~

Avatar by lebb
Reply With Quote
  #6  
Old 04-12-2005, 06:32 PM
larrylaffer133 larrylaffer133 is offline
Freshman
 
Join Date: Feb 2005
Posts: 30
Default

ok so it really isnt a necessary command? also how do you put a program in fullscreen mode without the window. then by hitting esc or something it gets out.
Reply With Quote
  #7  
Old 04-12-2005, 06:34 PM
reboot's Avatar
reboot reboot is offline
Keeper of foo

Retired Moderator
* Guru *
 
Join Date: Nov 2001
Location: Graceland
Posts: 15,612
Default

It's not a command. VB has no 'commands'. And yes, it's very necessary.
__________________
~ Quod non mortiferum, fortiorem me facit ~

Avatar by lebb
Reply With Quote
  #8  
Old 04-12-2005, 06:40 PM
larrylaffer133 larrylaffer133 is offline
Freshman
 
Join Date: Feb 2005
Posts: 30
Default

im still kind of confused though. why is it necessary, and what about the full screen mode thing.
Reply With Quote
  #9  
Old 04-12-2005, 06:40 PM
jpaugh78's Avatar
jpaugh78 jpaugh78 is offline
Senior Contributor
 
Join Date: Dec 2003
Location: Columbus, Ohio USA
Posts: 1,129
Default

Quote:
Originally Posted by larrylaffer133
....also how do you put a program in fullscreen mode without the window. then by hitting esc or something it gets out.
Well, you'd have to use the keypress event to see when the user presses the escape key. But to make it full screen without a window, i believe you just change the border setting of the form, and make the windowState maximized.
__________________
Did you Google your question before posting it here? Remember, Google is your friend. ;)
Reply With Quote
  #10  
Old 04-12-2005, 06:43 PM
reboot's Avatar
reboot reboot is offline
Keeper of foo

Retired Moderator
* Guru *
 
Join Date: Nov 2001
Location: Graceland
Posts: 15,612
Default

If you don't have Option Explicit, and you misspell a variable, VB doesn't complain, likely causing you no end of trouble. This has been explained once already.
__________________
~ Quod non mortiferum, fortiorem me facit ~

Avatar by lebb
Reply With Quote
  #11  
Old 04-12-2005, 06:46 PM
larrylaffer133 larrylaffer133 is offline
Freshman
 
Join Date: Feb 2005
Posts: 30
Default

oh ok, i understand. so if you set variables and then go to add a number to a certain variable but you misspell the variable vb will tell you there is no variable that exists with that name? otherwise it wouldnt say anything and it would take a long time to find out the problem.
Reply With Quote
  #12  
Old 04-12-2005, 06:53 PM
cool_dude cool_dude is offline
Centurion
 
Join Date: Mar 2005
Location: 3rd Rock From The Sun
Posts: 190
Default

set your form BorderStyle to 0 - None
set your form WindowState to 2 - Maximized

and add this code

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 27 Then
Unload Me
End If
End Sub

27 stands for escape
if you press escape it will close your form
__________________
Signature space for rent.
Reply With Quote
  #13  
Old 04-12-2005, 07:00 PM
larrylaffer133 larrylaffer133 is offline
Freshman
 
Join Date: Feb 2005
Posts: 30
Default

Oh awesome. What is the keycode for enter? Is there a keycode list so i can see the codes for all the keys on the keyboard?
Reply With Quote
  #14  
Old 04-12-2005, 07:09 PM
reboot's Avatar
reboot reboot is offline
Keeper of foo

Retired Moderator
* Guru *
 
Join Date: Nov 2001
Location: Graceland
Posts: 15,612
Default

vbKeyEnter, vbKeyEscape, etc
__________________
~ Quod non mortiferum, fortiorem me facit ~

Avatar by lebb
Reply With Quote
  #15  
Old 04-12-2005, 07:14 PM
webpsycho's Avatar
webpsycho webpsycho is offline
Regular
 
Join Date: Apr 2005
Location: The Faroe Islands
Posts: 70
Default

you can easily check it by adding a textbox to your form and setting this code to your textbox:

Code:
Public Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer) MsgBox "Key Pressed: " & KeyCode End Sub
__________________
"Commit Suicide Or Die Trying"

- "What do you give an elephant with diarrhoea?"
- "Lots of space"
Reply With Quote
  #16  
Old 04-12-2005, 07:19 PM
cool_dude cool_dude is offline
Centurion
 
Join Date: Mar 2005
Location: 3rd Rock From The Sun
Posts: 190
Default

Quote:
Originally Posted by reboot
vbKeyEnter, vbKeyEscape, etc

this only returns an error


If KeyCode = 13 Then
Unload Me
End If

this will do the trick
note: 13 is enter button
__________________
Signature space for rent.
Reply With Quote
  #17  
Old 04-12-2005, 07:20 PM
larrylaffer133 larrylaffer133 is offline
Freshman
 
Join Date: Feb 2005
Posts: 30
Default

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If vbKeyEnter Then
Unload Me
End If
End Sub

this isnt working. sorry to be annoying, im just new to this.
Reply With Quote
  #18  
Old 04-12-2005, 07:22 PM
reboot's Avatar
reboot reboot is offline
Keeper of foo

Retired Moderator
* Guru *
 
Join Date: Nov 2001
Location: Graceland
Posts: 15,612
Default

Sorry, I misspoke. It's vbKeyReturn... And its value is also 13. Constants are better. Learn to use them.
__________________
~ Quod non mortiferum, fortiorem me facit ~

Avatar by lebb
Reply With Quote
  #19  
Old 04-12-2005, 07:30 PM
cool_dude cool_dude is offline
Centurion
 
Join Date: Mar 2005
Location: 3rd Rock From The Sun
Posts: 190
Default

check this out if you need more key codes
use the decimal section for key codes
i attached it
__________________
Signature space for rent.

Last edited by cool_dude; 04-12-2005 at 07:33 PM. Reason: forgot to attach a file :)
Reply With Quote
  #20  
Old 04-12-2005, 08:27 PM
HardCode's Avatar
HardCode HardCode is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Feb 2004
Location: New Jersey
Posts: 3,338
Default

Why would you use that list when it goes against reboot's solid advice of using the constants?

http://msdn.microsoft.com/library/de...eConstants.asp
__________________
DON'T CLICK HERE

Useful forum tags: [VB][/VB], [CODE][/CODE], [HTML][/HTML]
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->