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


Reply
 
Thread Tools Display Modes
  #1  
Old 09-18-2002, 06:24 PM
Rotti
Guest
 
Posts: n/a
Default Option explicit


Hello, guys,

Something about VB code I always confused myself, it's the Option Explicit on the top of every thing, could you pros give me a brief explanation?

1/. When I added this to the code, the one that worked would no longer work, mostly to do with var names
2/. When and how should I use it?
3/. What is the purpose of this?
4/. Any limitation on this?

Thanks a lot, guys.
Reply With Quote
  #2  
Old 09-18-2002, 06:27 PM
Spike's Avatar
Spike Spike is offline
Senior Contributor
 
Join Date: Jan 2000
Location: Oregon
Posts: 1,008
Default

1. It requires all variables to be declared
2. Always
3. To make sure you declare all your variables
4. What do you mean?
__________________
Spike
-Sound clip of the week-
The ashes of the dead will be a sign | The time approaches
Killswitch Engage - Numbered Days
Reply With Quote
  #3  
Old 09-18-2002, 06:33 PM
Rotti
Guest
 
Posts: n/a
Default

I mean if there is any time I should not use this.
Why should I always use this? eg, if I put a message box:

MyMessage = MsgBox("Hello World", 0, "Test")

That means I have to declare MyMessage as a var first?
Wouldn't that be a hassle? In the case I had above, what type of var I should declare as?

Thanks a lot.
Reply With Quote
  #4  
Old 09-18-2002, 06:51 PM
Spike's Avatar
Spike Spike is offline
Senior Contributor
 
Join Date: Jan 2000
Location: Oregon
Posts: 1,008
Default

Well, not declaring variables can lead to messy code, hard to find errors, and slow code.

In that senario, i would declare MyMessage as an Integer.
__________________
Spike
-Sound clip of the week-
The ashes of the dead will be a sign | The time approaches
Killswitch Engage - Numbered Days
Reply With Quote
  #5  
Old 09-18-2002, 06:52 PM
randtek's Avatar
randtek randtek is offline
Contributor
 
Join Date: Jun 2002
Location: the Milky Way Galaxy
Posts: 525
Default

For one thing, declaring all variables makes debugging easier, as it prevents errors caused by mistyping or misspelling variable names.

It really is not a hassle once you get in the habit. And it's much less hassle than struggling to find a strange error caused by a mistyped variable name. It is also just considered to be good programming practice.
__________________
Murphy was an optimist!
Reply With Quote
  #6  
Old 09-18-2002, 06:54 PM
orufet's Avatar
orufet orufet is offline
Paranoid Coder

Retired Leader
 
Join Date: Mar 2001
Location: Canada
Posts: 2,716
Default

You can also declare MyMessage as vbMsgBoxResult.
Reply With Quote
  #7  
Old 09-18-2002, 06:55 PM
Rotti
Guest
 
Posts: n/a
Default

Thanks a lot, guys!
Reply With Quote
  #8  
Old 09-18-2002, 08:05 PM
MarkVB6's Avatar
MarkVB6 MarkVB6 is offline
Centurion
 
Join Date: Aug 2002
Location: Ontario, Canada
Posts: 170
Default Further question

What happens when you want to use the split command?

Because if you have to declare the variable that the split array is going to be placed into, what should you declare it as? An array won't work, and neither will a non-array. So how would you work split around Option Explicit?
__________________
A silly idea is current that good people do not know what temptation means. This is an obvious lie. Only those who try to resist temptation know how strong it is... A man who gives in to temptation after five minutes simply does not know what it would have been like an hour later. That is why bad people, in one sense, know very little about badness. They have lived a sheltered life by always giving in. -C.S. Lewis
Reply With Quote
  #9  
Old 09-18-2002, 08:13 PM
Pookie's Avatar
Pookie Pookie is offline
Gaming God

Retired Leader
* Expert *
 
Join Date: Feb 2002
Location: Brisbane, Australia
Posts: 2,363
Default

An example on how to use split
Code:
Option Explicit Dim strString as String Dim Data() as String strString="A B C D E" Data = Split(strString," ") Debug.Print Data(3)
Result of Debug.Print = "D"
__________________
. <--- Photo of me viewed from the Hubble telescope
Reply With Quote
  #10  
Old 09-18-2002, 08:20 PM
MarkVB6's Avatar
MarkVB6 MarkVB6 is offline
Centurion
 
Join Date: Aug 2002
Location: Ontario, Canada
Posts: 170
Default

Ahh i see you just leave a blank array like Data(). Never learned those kind of basics...though I knew how to use split.
__________________
A silly idea is current that good people do not know what temptation means. This is an obvious lie. Only those who try to resist temptation know how strong it is... A man who gives in to temptation after five minutes simply does not know what it would have been like an hour later. That is why bad people, in one sense, know very little about badness. They have lived a sheltered life by always giving in. -C.S. Lewis
Reply With Quote
  #11  
Old 09-18-2002, 08:22 PM
Pookie's Avatar
Pookie Pookie is offline
Gaming God

Retired Leader
* Expert *
 
Join Date: Feb 2002
Location: Brisbane, Australia
Posts: 2,363
Default

A blank data in an array means it gets declared dynamically, which mean you can change it's size at any time.
eg:
Code:
Dim a() as Long Redim a(9) ' give it 10 elements Redim Preserve a(4) ' give it 5 elements, can keep the info Erase a() ' release the memory
__________________
. <--- Photo of me viewed from the Hubble telescope
Reply With Quote
  #12  
Old 09-19-2002, 02:47 AM
BillSoo's Avatar
BillSoo BillSoo is offline
Code Meister

Retired Moderator
* Guru *
 
Join Date: Aug 2000
Location: Vancouver, BC, Canada
Posts: 10,441
Default

And if you really wanted to...you could define a VARIANT explicitly. Without OPTION EXPLICIT, any unknown variable was assumed to be a variant. But you could just say:

dim v as variant

v = split(sString," ")

and v will be a variant that contains the string array.

However, a lot of people frown on variants, myself included. It's usually better to simply define the type you need instead of relying on the behaviour of a variant.
__________________
"I have a plan so cunning you could put a tail on it and call it a weasel!" - Edmund Blackadder
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
 
 
-->