0

What's the main problem if I don't declare the type of a variable? Like, Dim var1 versus Dim var1 as Integer.

4

5 回答 5

7

The main reason to declare variable types in languages which allow you to use variant types is as a check on yourself. If you have a variable that you're using to hold a string, and then by accident you pass it to a function that expects an integer, the compiler can't tell you that you messed up unless you told it that that variable was supposed to always be a string. Instead, you're stuck with your string being reinterpreted as an integer, which is pretty much never going to give you what you want and the results will likely be confusing, and it will be hard to track down the bug.

In pretty much all languages there are a lot of constructs where you could leave it out and your program would work, but exist as a check on the programmer. The first job of a compiler is to make your code into an executable. But the second job of the compiler is to try as much as possible to make sure that the programmer didn't make a mistake. Especially when your program gets large, it's easier to let the compiler find mistakes like this as opposed to trusting that you typed everything exactly right.

Additionally, there is usually some processing overhead associated with variants, but this is a more minor concern.

于 2009-03-17T17:25:16.260 回答
3

There are a few reasons:

  • Vastly improved type safety.
  • Lower cognitive overhead; the compiler and Intellisense can help you.
  • Lower performance overhead; transforming things to and from Variant types has a small but nontrivial cost.
  • Eliminates need for naming warts (e.g., lblTitle to tell you that something is supposed to hold a Label).
  • Moves some kinds of runtime errors to compile-time errors, which is a big productivity win.
于 2009-03-17T17:28:42.867 回答
1

Someone else already mentioned Intellisense, but but it's worth repeating.

Additionally, when you declare an explicit type for your variable you make it possible for the compiler to do all kinds of extra type checking and validation on your code that would not otherwise be possible. What happens is that now certain kinds of very common error are caught and fixed at compile time rather than run time. The user never sees them. You don't want to leave errors for run-time.

You say "it could be anything" — and that is true. But you then go on to say "so it must be good". That doesn't necessarily follow, and it's very dangerous. Not everything can be assigned or combined with everything else. It could be anything — but it is something, or rather some specific thing. Without an explicit type, the compiler has no way to know what and can't help you avoid errors.

于 2009-03-17T17:23:54.267 回答
0

Superficially, if you don't declare the type, Intellisense can't help you with it because it doesn't know what type it is.

于 2009-03-17T17:20:04.790 回答
0

Contrast this to Python's typing system. Python allows a developer to use a variable without declaring type in advance but once a variable is used, the type is fixed. A variant, by contrast, can be assigned any type of value initially and a different type can be stored later on without any warning or complaint. So you can put a string into a variable that previously held a numeric.

Dim myvar1
myvar1 = 1

'A whole lot more code 

myvar1 = "this string"

If you ever have to maintain someone else's code, you'll start to understand why this sort of thing (changing a variable type silently) can be extra tough to maintain. Especially if you're using a module level variable this could lead to some really interesting problems. This is along the same lines as using Option Explicit in VB code. Without Option Explicit you can do this sort of thing without realizing it:

myvar1 = 1

'A whole lot more code here too

myvarl = 2

In some fonts those two variable names would be impossible to distinguish and this could lead to some tough to find bugs.

于 2009-03-18T01:22:43.050 回答