JMK
JMK

Reputation: 28079

Should I explicitly declare my variables in VB6

I'm writing some code in Visual Basic 6 and I have noticed that I don't even need to declare variables for things to work.

The following (explicit declaration):

Dim foo As String
foo = "Bar"

Seems to work just as well as this (implicit declaration):

Dim foo
foo = "Bar"

Or this (no declaration):

foo = "Bar"

I know in C# I need to declare a variable before I use it, and that implicit and explicit declarations are both acceptable. I also know that in Python, you don't declare your variables at all before you use them.

In regards to Visual Basic 6 (and by extension VBA) which is proper?

Thanks

Upvotes: 5

Views: 3253

Answers (4)

Siddharth Rout
Siddharth Rout

Reputation: 149335

Should I explicitly declare my variables in VB6?

Yes. Why?

Not just because it is a good habit or it is a must but because of only one main reason which I have mentioned in this post as well.

VB defaults the variable to being type Variant. A Variant type variable can hold any kind of data from strings, to integers, to long integers, to dates, to currency etc. By default “Variants” are the “slowest” type of variables.

AND

As I mentioned earlier, If you do not specify the type of the variable, VB defaults the variable to being type Variant. And you wouldn’t want that as it would slow down your code as the VB Compiler takes time to decide on what kind of variable you are using. Variants should also be avoided as they are responsible for causing possible “Type Mismatch Errors”.

Topic: To ‘Err’ is Human (See Point 3)

Link: http://siddharthrout.wordpress.com/2011/08/01/to-err-is-human/

The above link also covers other parts related to coding that one can/should take care of.

HTH

Upvotes: 7

GTG
GTG

Reputation: 4954

I highly reccomend that you always declare your variables. This can be forced by setting Option Explicit in each code module. You can let VB6 do that automatically for you by going to Tools->Options, in the Editor tab check Require variable declaration.

If you don't use Option Explicit, then a variable will be automatically created for you each time you reference a previously unknown variable name. This is a very dangerous behavior, because if you mistype a variable name, an empty variable will be created for you, causing unexpected behavior of your code.

You don't have to declare the type of your variables but I would also recommend that you do that. The default type of a variable is Variant, which has a small performance overhead and create some problems if you are creating COM objects for use by C++ or C# (if anybody does that anymore).

Upvotes: 4

JeffK
JeffK

Reputation: 3039

In Tools/Options, Editor tab, check the Require Variable Declaration checkbox. This will automatically add Option Explicit to every new code module.

enter image description here

I would say this is more than a best practice; I think of it as a requirement for programmer sanity. The setting is persistent; once set, it stays enabled. Microsoft made it an option because some older versions of VB didn't have the feature, which also explains why it was disabled by default.

Upvotes: 9

Willow Anne
Willow Anne

Reputation: 91

It's a good HABIT.

There is a VB option called Option Explicit. With that set to ON, then VB forces you to declare a variable before you use it: no more

foo = "Bar"

That helps with mistyping the variable name later in your code... without that, you can typso the variable name, your program compiles but won't work, and it's HARD to dig that out.

Upvotes: 9

Related Questions