1
Static i as integer

What will i be before I assign a value.

It seems to be just zero (0) but I wanted to confirm that.

Clay Nichols
  • 11,848
  • 30
  • 109
  • 170
  • Just in case it isn't clear, `Static` in VB6 has an entirely different meaning from its meaning in .Net. A static variable is a local variable that retains its value between method calls. In other words, it has the scope of a local variable but the lifetime of a module-level variable. – BobRodes May 06 '16 at 07:02
  • 1
    @BobRodes It is used in the [exactly same way](https://msdn.microsoft.com/en-us/library/z2cty7t8.aspx) in VB.NET. You are probably thinking [C#](https://msdn.microsoft.com/en-us/library/98f28cdx.aspx) but then it's not correct to compare keyword use in two different languages or use ".NET" in place of "C#". – GSerg May 06 '16 at 07:19
  • @GSerg Quite right, I stand corrected. – BobRodes May 07 '16 at 01:46

2 Answers2

3

Variables of all VB data types receive their respective default value when the procedure starts.

This includes initializing all numbers to zero, and all the other data types to their flavour of zero (vbNullString for strings, not exactly the same as an empty string "", False for booleans, Empty for variants, and Nothing for objects).

GSerg
  • 76,472
  • 17
  • 159
  • 346
  • The Debug statement `? vbNullstring = ""` evaluates to `True`. So, maybe not *exactly* the same as an empty string, in the same sense that a constant isn't *exactly* the same as its value. But for all practical purposes, I would say that they are exactly the same, since they are interchangeable in code. – BobRodes May 06 '16 at 06:57
  • 3
    @BobRodes In VB `=` evaluates to `True` not only when the two things are the *same*, but also when they can be coerced to something else that can be seen as same. E.g. `? False = 0` or `? False = Empty` all give `True` even though they are not the same, rather, they all are [falsy](https://developer.mozilla.org/en/docs/Glossary/Falsy). `vbNullString` is different though, because it has the same type as `""` (`String`), and they are actually the *same* if you are only interested in comparing their content, but sometimes you [want to know more](http://stackoverflow.com/a/20909528/11683). – GSerg May 06 '16 at 07:13
  • 2
    There can be a difference in the results you get when passing `vbNullString` vs. `""` to some methods, properties, etc. In some cases passing `vbNullString` can even cause a null pointer exception. – Bob77 May 09 '16 at 04:14
3

According to Microsoft

Normally in Visual Basic, when a static variable is declared inside a Function or Sub procedure, it gets initialized to 0 (numeric data type) or an empty string, "" (string data type), by default.

So yeah, you can be sure it's default value is zero.

Hope this helps

coffee_addict
  • 926
  • 9
  • 15
  • VB6 documentation on MSDN, https://msdn.microsoft.com/en-us/library/aa243352(v=vs.60).aspx – jac May 04 '16 at 21:38