What does vb6 initialize a static Integer to?
Asked Answered
vb6
M

2

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.

Misreckon answered 4/5, 2016 at 18:54 Comment(3)
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.Grindle
@Grindle It is used in the exactly same way in VB.NET. You are probably thinking C# but then it's not correct to compare keyword use in two different languages or use ".NET" in place of "C#".Brindled
@Brindled Quite right, I stand corrected.Grindle
B
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).

Brindled answered 4/5, 2016 at 19:0 Comment(3)
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.Grindle
@Grindle 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. 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.Brindled
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.Botsford
N
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

Novation answered 4/5, 2016 at 19:0 Comment(1)
VB6 documentation on MSDN, msdn.microsoft.com/en-us/library/aa243352(v=vs.60).aspxShadowy

© 2022 - 2024 — McMap. All rights reserved.