preventing default value in vb.net

J

Justin

Is there any way to prevent VB.net to give default value to a variable
(changing settings what not...)?

For example


Dim test As Integer
test = test + 1

the statement "test = test + 1" should give me an error since variable test
was never initialized. I want to prevent giving default value of 0.

Thanks
 
L

Larry Lard

Justin said:
Is there any way to prevent VB.net to give default value to a variable
(changing settings what not...)?

For example


Dim test As Integer
test = test + 1

the statement "test = test + 1" should give me an error since variable test
was never initialized. I want to prevent giving default value of 0.

No. From the specification of VB (my emphasis):

"
4.8 Variables
A variable represents a storage location. Every variable has a type
that determines what values can be stored in the variable. Because
Visual Basic is a type-safe language, every variable in a program has a
type and the language guarantees that values stored in variables are
always of the appropriate type. ***Variables are always initialized to
the default value of their type before any reference to the variable
can be made.*** It is not possible to access uninitialized memory.
"
The default value for an Integer is, as you know, zero.
 
M

Michel Posseth [MCP]

To be short No this is not possible

The only language i know of tat behaves the way you described is Javascript

by the way oftopic but maybe nice to know

you did
Dim test As Integer
test = test + 1


i would say

Dim test As Integer
test += 1

saves you some typing and behaves the same :)

regards

Michel Posseth
 
C

Chris Dunaway

You might be able to set VS up to flag that as a warning in the IDE.
Check the compile properties for the project and see if it will let you
do this.
 
H

Herfried K. Wagner [MVP]

Chris Dunaway said:
You might be able to set VS up to flag that as a warning in the IDE.
Check the compile properties for the project and see if it will let you
do this.


ACK, this should work (and is the default behavior) in VS 2005. However, I
have turned off the warning because I find the warning useless in 99 % of
its occurances. 'Dim x As Integer' is simply the same as 'Dim x As Integer
= Nothing', with 'Nothing' referring to the type's default value which is 0
in VB, and thus equivalent to 'Dim x As Integer = 0'. So why type 'Dim x As
Integer = 0' if 'Dim x As Integer' is already doing that?
 
L

Larry Lard

Herfried said:
ACK, this should work (and is the default behavior) in VS 2005.

Not for value types. The compile setting that can be warned or errored
on is 'Use of variable prior to assignment' - however as the spec
extract I quoted earlier shows, value types *always* have a value, even
when they haven't been explicitly assigned one, and this value is
always 'safe'.

You can try it yourself:

(on My Project | Compile, set 'Use of variable prior to assignment' to
be a warning), then:

Dim x As Integer
Dim o As Object

Console.WriteLine(x.ToString)
Console.WriteLine(o.ToString)

Only one warning.

However, I
have turned off the warning because I find the warning useless in 99 % of
its occurances. 'Dim x As Integer' is simply the same as 'Dim x As Integer
= Nothing', with 'Nothing' referring to the type's default value which is 0
in VB, and thus equivalent to 'Dim x As Integer = 0'. So why type 'Dim x As
Integer = 0' if 'Dim x As Integer' is already doing that?

Since such a warning would be useless 100% of the time _for value
types_, the compiler doesn't issue one :)
 
C

Cor Ligthert [MVP]

Justin,
the statement "test = test + 1" should give me an error since variable
test was never initialized. I want to prevent giving default value of 0.

Did you test it?

In my idea it is initialized at the first use

test = test ' gives zero

Cor
 
H

Herfried K. Wagner [MVP]

Larry Lard said:
Not for value types. The compile setting that can be warned or errored
on is 'Use of variable prior to assignment' - however as the spec
extract I quoted earlier shows, value types *always* have a value, even
when they haven't been explicitly assigned one, and this value is
always 'safe'.

You are right. I have mixed it up with something else because it was the
first thing I have turned off.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top