if (var != Undefined.Value) does NOT work

  • Thread starter Thread starter Steven Prasil
  • Start date Start date
S

Steven Prasil

I have a C# program with an if statement similar to

if (obj.myvar != Undefined.Value) { ...... }

When I test it in the CSharp CLR debugger this debugger does not recoginze
that the value is currently <undefined value> as shown in the watch list of the Debugger.
Hence the program branches into the if statement although it should skip it
obviously. Why?

Replacing the if statement above by

if (obj.var != null) { ......}

does NOT help. The programm jumps INTO the if branch.

Steven
 
Steven Prasil said:
if (obj.myvar != Undefined.Value) { ...... }
[...] debugger does not recoginze that the value is
currently <undefined value> [...] Hence the program
branches into the if statement

If the value is undefined, I'd expect it to branch into the if block,
because you're using the != (not equal) operator.

P.
 
Steven Prasil said:
I have a C# program with an if statement similar to

if (obj.myvar != Undefined.Value) { ...... }

When I test it in the CSharp CLR debugger this debugger does not recoginze
that the value is currently <undefined value> as shown in the watch list of the Debugger.
Hence the program branches into the if statement although it should skip it
obviously. Why?

Replacing the if statement above by

if (obj.var != null) { ......}

does NOT help. The programm jumps INTO the if branch.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
Hi Steven,

Debugger shows <undefined value> whenever reference variable is set to
*null*. For value types it won't show <undefined value>. In other words
*null* is <undefined value> for the debugger.
There is not Undefined.Value class and proerty if this is what you were
trying.

In the first example you posted you test *obj.myvar* in the second
*obj.var*. It might be the reason of the wrong result.
 
chandra mohan said:
try if(obj.var == null)

When is null != null ?

I have experienced similar problems. The code worked as expected in
non-debug, yet in debug it failed to evaluate correctly.

Try this and see if it helps:

Shutdown Visual Studio.
Delete the contents of your build folders (bin and obj).
Try again.

Andrew Cowan
Devtest Pty Ltd
 
Back
Top