 |

09-18-2002, 06:24 PM
|
|
|
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.
|
|

09-18-2002, 06:27 PM
|
 |
Senior Contributor
|
|
Join Date: Jan 2000
Location: Oregon
Posts: 1,008
|
|
|
1. It requires all variables to be declared
2. Always
3. To make sure you declare all your variables
4. What do you mean?
|
|

09-18-2002, 06:33 PM
|
|
|
|
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.
|
|

09-18-2002, 06:51 PM
|
 |
Senior Contributor
|
|
Join Date: Jan 2000
Location: Oregon
Posts: 1,008
|
|
|
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.
|
|

09-18-2002, 06:52 PM
|
 |
Contributor
|
|
Join Date: Jun 2002
Location: the Milky Way Galaxy
Posts: 525
|
|
|
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!
|

09-18-2002, 06:54 PM
|
 |
Paranoid Coder
Retired Leader
|
|
Join Date: Mar 2001
Location: Canada
Posts: 2,716
|
|
|
You can also declare MyMessage as vbMsgBoxResult.
|
|

09-18-2002, 06:55 PM
|
|
|
|

09-18-2002, 08:05 PM
|
 |
Centurion
|
|
Join Date: Aug 2002
Location: Ontario, Canada
Posts: 170
|
|
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
|

09-18-2002, 08:13 PM
|
 |
Gaming God
Retired Leader * Expert *
|
|
Join Date: Feb 2002
Location: Brisbane, Australia
Posts: 2,363
|
|
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
|

09-18-2002, 08:20 PM
|
 |
Centurion
|
|
Join Date: Aug 2002
Location: Ontario, Canada
Posts: 170
|
|
|
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
|

09-18-2002, 08:22 PM
|
 |
Gaming God
Retired Leader * Expert *
|
|
Join Date: Feb 2002
Location: Brisbane, Australia
Posts: 2,363
|
|
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
|

09-19-2002, 02:47 AM
|
 |
Code Meister
Retired Moderator * Guru *
|
|
Join Date: Aug 2000
Location: Vancouver, BC, Canada
Posts: 10,441
|
|
|
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
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|