|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
preventing default value in vb.netIs 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 Justin wrote:
> Is there any way to prevent VB.net to give default value to a variable No. From the specification of VB (my emphasis):> (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. " 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 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 > 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. "Chris Dunaway" <dunaw***@gmail.com> schrieb: ACK, this should work (and is the default behavior) in VS 2005. However, I > 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. 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/> Herfried K. Wagner [MVP] wrote:
> "Chris Dunaway" <dunaw***@gmail.com> schrieb: Not for value types. The compile setting that can be warned or errored> > 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. 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 Since such a warning would be useless 100% of the time _for value> 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? types_, the compiler doesn't issue one :) -- Larry Lard Replies to group please "Larry Lard" <larryl***@hotmail.com> schrieb: You are right. I have mixed it up with something else because it was the >> > 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'. 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/> Justin,
> the statement "test = test + 1" should give me an error since variable Did you test it?> test was never initialized. I want to prevent giving default value of 0. 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 >
Wrapper classes
FAST date string conversion ? playing & re-playing 7 .wav files How To Force Copy One File To Another Possible to monitor changes in array (with event)? Debug.WriteLine Doesn't Produce Output Problems with URL Syntax Raising Events in Modules? Re: How to Use a Screen Saver app within My Application? word automation |
|||||||||||||||||||||||