1

So I have this:

Dim aBoolean As Boolean = True

Will it make any difference to just do this?

Dim aBoolean = True

In other languages, I believe it was a good practice to also define the type of variable it would be, for performance or something. I am not entirely sure with VB.NET.

Thanks.

Saturn
  • 17,888
  • 49
  • 145
  • 271

4 Answers4

5

As long as you have Option Infer turned on, it won't make a bit of difference. The second line is just a syntactic abbreviation for the first. At that point, it's up to your style preference as to which you should use.

Adam Maras
  • 26,269
  • 6
  • 65
  • 91
  • @Adam: `Dim aBoolean = True` will still type `aBoolean` as a `Boolean` with `Option Infer On`, even with `Option Strict Off`. – Dan Tao Jan 27 '11 at 20:16
  • @Dan: I'm not sure where it made it into my head that inference only worked with `Strict On`. Thanks for the correction; I've removed my comment. – Adam Robinson Jan 27 '11 at 20:30
  • I always think it's funny when someone removes a comment after someone has responded to it. This one is particularly amusing. Is Dan talking to Adam Robinson or Adam Maras? Obviously, Adam Robinson's second comment clears this up, but it's still funny. – Ben McCormack Jan 27 '11 at 20:37
  • @Ben, @Adam: I must agree with Ben; now this exchange has become rather odd-looking. Should we just delete all of these? I'd be fine with that. – Dan Tao Jan 27 '11 at 20:42
  • @Dan I wouldn't worry about it. Maybe I enjoy the confusion too much :-) – Ben McCormack Jan 27 '11 at 21:04
5

It depends. Explicitly defining the variable can improve readability, but I don't think it's always necessary. (To be clear, it has nothing to do with the actual functionality of your code).

In this specific example, you follow the declaration with a Boolean assignment of True, so it's already crystal clear that aBoolean is actually a Boolean when it is declared. The As Boolean syntax is not as necessary in this scenario.

Other cases may not be so clear. If the declaration was followed by the result of a function call, for example, it might be more clear to explicitly declare that the variable is a Boolean. e.g.

Dim aBoolean As Boolean = TestValidityOfObject(o)
Ben McCormack
  • 32,086
  • 48
  • 148
  • 223
3

Before type inference, there were performance issues when not declaring the type, but that's no longer an issue; due to type inference the variable will be of type Boolean whether you declare it or not.

Declaring the type can help the compiler catch errors sooner, and will often give you better Intellisense.

Jacob Mattison
  • 50,258
  • 9
  • 107
  • 126
3

You're using what's called "type inference". This is where the compiler figures out at compile time what the type on the right side of the assignment is and uses that as the type of the variable.

This is, in general, a safe and convenient feature. However, there are a couple of things to keep in mind:

  1. You must have Option Infer on; otherwise, the compiler doesn't do type inference and, depending on your setting for Option Strict, instead either gives you a compile time error (Option Strict On) or types your variable as Object and uses late binding everywhere. This is Pure Evil. (Option Strict Off)
  2. In your particular case, there's no way for the compiler to mess up. HOWEVER, it's possible to use type inference in such a way as to change the semantics of your code:

For instance...

Dim myClass as MyBaseClass = New SubClass()

This is perfectly legal; we're typing the variable as a base class and assigning a value to it that represents an instance of a subclass. Nothing special. However, if we switch to type inference by just removing the type declaration...

Dim myClass = New SubClass()

Type inference will now see myClass as a SubClass instead of MyBaseClass. This might seem obvious, but the point is that you should be aware of what it's doing.

For more information and long-winded discussion about using type inference, see this question. While that question is targeted at C#, the only real difference is the first item that I listed above. Everything else is conceptually the same.

Community
  • 1
  • 1
Adam Robinson
  • 182,639
  • 35
  • 285
  • 343