17

Consider the declarations

Dim x As Double
Dim avg(1 To 10) As Double

What are the default values in x and avg?

Through testing, I want to say that x is initialized to zero. Likewise, I want to say that all elements in avg were initialized to zero.

However, can I write code that depends on this? Or are the default initialization values actually indeterminate?

Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
Prashant Kumar
  • 20,069
  • 14
  • 47
  • 63

3 Answers3

20

If you specify a data type but do not specify an initializer, Visual Basic initializes the variable to the default value for its data type, which for all numeric types is 0 (zero).

When you run a macro, all the variables are initialized to a value. A numeric variable is initialized to zero, a variable length string is initialized to a zero-length string (""), and a fixed length string is filled with the ASCII code 0. Variant variables are initialized to Empty. An Empty variable is represented by a zero in a numeric context and a zero-length string ("") in a string context.

Ref. Link dead.

Instead: How to use variables in Excel sub-procedures in Visual Basic for Applications

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
  • 1
    Pretty sure this is correct. Note, however, that if you use dynamic arrays no elements are initialized and referencing arr(0) before explicitly setting it returns an error. – dmogle Oct 02 '11 at 17:43
  • 1
    An aside, I'm pretty sure (by searching SO) that it isn't possible to specify an initializer at declaration in Excel VBA, since that's a .NET thing. Thank you for the Ref! That totally dispels any doubt that `x` holds zero. Though is it the case that every element in a fixed array is also initialized to zero? I actually had already discovered that dynamic arrays don't get initialized (only allocated) upon declaration, and I just wanted to make sure fixed arrays declaration didn't behave the same way. – Prashant Kumar Oct 02 '11 at 21:33
  • @Prashant, why not add a watch to the variable and test for yourself? – Reafidy Oct 03 '11 at 03:30
4

You can write code that implicitly depends on a given data type's default value, but that doesn't mean you always should, because it isn't quite as obvious what you're doing, which may confuse the next person reading your code (and that person could be you a year from now).

You can't go wrong with explicitly assigning an initial value to your variables. If you want, you can do that on the same line as the declaration:

Dim x As Double: x = 0

Of course, it's a little more involved for arrays, but usually there's a convenient place to initialize them further down in your code, where you're traversing them anyway.

Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
2

Yes, you should be able to depend on number types being initialize to 0 in VBA since that's their default value.

However, if it makes you worry, and the starting values are so important, then my reccomendation is that you just explicitly assign the value 0 to them (although you really don't need to).

Gaijinhunter
  • 14,587
  • 4
  • 51
  • 57