views:

35

answers:

2

I can make the compiler give me an error (Use of variable prior to assignment) with:

private sub Test()
   Dim ord As Order
   Dim ord2 As Order
   ord2 = ord
end sub

but not with:

Friend Class frmReceiving
...

Private mobjOrder As Order 

...

private sub Testing()
   Dim ord2 As Order
   ord2 = mobjOrder 
end sub

How can I make it flag as error?

thanks.

A: 

Yes.

Use the /warnaserror compiler option.

http://msdn.microsoft.com/en-us/library/2xz9dxe5.aspx

Vitor Py
A: 

Your second example is not an error. mobjOrder will be initialized to Nothing. You then assign Nothing to ord2. That's a perfectly legitimate thing to do.

John Saunders
what's the diff between the 2 examples. why does mobjOrder get intialized to Nothing, but not ord in the first example?
In the first example, it's a local variable. In the second example, it's an instance variable.
John Saunders
ok, I see now after thinking about it. There IS no difference in the 2 assignments, the 2 objects are "Nothing", it just must be the compiler has no way of knowing if the private class member object has been intialized elsewhere prior to the assignment statement (though I would wish it could analyze all the places where it is used and determine if it is), whereas in the method it knows the local variable is unitialized.
MarkJ
@MarkJ: I don't know VB to that level of detail, but in C#, `ord` would _not_ be initialized to `null`. It would simply be a compiler error.
John Saunders
@John I hadn't realised that C# requires that all local variables must be initialised in a way that the compiler can detect through static analysis (just found the rules in the language spec). I'm not sure whether VB requires that.
MarkJ