How can I force a compile-time warning in VB.NET when using an unassigned local variable? -


today discovered had assumed vb.net many years not true (worrying!). assumed variable declared within loop had lifetime of iteration declared in, in fact seems has lifetime of whole procedure.

for example:

        integer = 0 1             dim var1 boolean             console.writeline(var1.tostring())             var1 = true             console.writeline(var1.tostring())         next         console.readkey() 

i had assumed output of false, true, false, true instead false, true, true, true.

in c# equivalent code not compile compile time error of error "use of unassigned local variable 'var1'".

i realise there many ways fix , best practice declare variable outside of loop , reset @ beginning of every loop through.

i find behaviour counter-intuitive me @ least compile time warning in vb.net when/if this. (i set on projects have , warning allow me check assumptions aren't causing errors).

does know how/if can generate compile time warning in vb.net? 1 finds counter-intuitive?

we'll have work on fixing intuition because getting error out of compiler not option. partially implemented, can warning:

error bc42104: variable 'mumble' used before has been assigned value. null reference exception result @ runtime.

and elevate warning error project + properties, compile tab. however, warning message indicates, supported reference type references, won't budge variable of value type.

okay, intuition. if runtime implement desired behavior have allocate new variable each iteration of loop. implies number of local variables bounded number of iterations. wasteful , easy trigger stackoverflowexception. jit compiler doesn't this, re-uses variable. happens in c# well, minus option of letting not initialize value explicitly of course.

fwiw: agree unhelpful behavior. you'll find receptive ears @ connect.microsoft.com, post feature request there , vb.net team see it. there has been strong backing customers within msft make vb.net , c# feature comparable. if post link feedback report i'll happy vote up.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -