Home All Groups Group Topic Archive Search About

preventing default value in vb.net

Author
25 May 2006 3:18 PM
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

Author
25 May 2006 3:54 PM
Larry Lard
Justin wrote:
> 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.

--
Larry Lard
Replies to group please
Author
25 May 2006 6:17 PM
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




Show quoteHide quote
"Justin" <jus***@hotmail.com> schreef in bericht
news:uPcZy5AgGHA.3468@TK2MSFTNGP03.phx.gbl...
> 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
>
Author
25 May 2006 8:44 PM
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.
Author
25 May 2006 10:21 PM
Herfried K. Wagner [MVP]
"Chris Dunaway" <dunaw***@gmail.com> schrieb:
> 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?

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
26 May 2006 8:38 AM
Larry Lard
Herfried K. Wagner [MVP] wrote:
> "Chris Dunaway" <dunaw***@gmail.com> schrieb:
> > 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.

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 :)

--
Larry Lard
Replies to group please
Author
26 May 2006 11:47 AM
Herfried K. Wagner [MVP]
"Larry Lard" <larryl***@hotmail.com> schrieb:
>> > 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.
>
> 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.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
26 May 2006 9:05 AM
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

Show quoteHide quote
"Justin" <jus***@hotmail.com> schreef in bericht
news:uPcZy5AgGHA.3468@TK2MSFTNGP03.phx.gbl...
> 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
>