TRUE IF-statement gets ignored

  • Thread starter Thread starter Gabriël
  • Start date Start date
G

Gabriël

Hi Folks,

If got a piece of code in which a IF-statement is true, but still the ELSE
code is excecuted. (While debuging it constantly jumps to the ELSE, while
the expression, according to the Command Window & Watch is true)

Code:

if (Request.QueryString["Id"] != null)

{

if (ViewState["action"] == "editing")

{

(..)

}

else

{

(..)

}

}



Any help would be appriciated!

Greets,

Gab
 
Hi,

The line
if (ViewState["action"] == "editing")

should give you a warning, as the left side is an object, not a string:

Possible unintended reference comparison; to get a value comparison, cast
the left hand side to type 'string'


Just do ViewState["action"].ToString()

Cheers,
 
Thanks for the two responses!

Still strange the compiler accepts it, the watch didn't complain and it did
run,

Greets,
Gab

Ignacio Machin ( .NET/ C# MVP ) said:
Hi,

The line
if (ViewState["action"] == "editing")

should give you a warning, as the left side is an object, not a string:

Possible unintended reference comparison; to get a value comparison, cast
the left hand side to type 'string'


Just do ViewState["action"].ToString()

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Gabriël said:
Hi Folks,

If got a piece of code in which a IF-statement is true, but still the ELSE
code is excecuted. (While debuging it constantly jumps to the ELSE, while
the expression, according to the Command Window & Watch is true)

Code:

if (Request.QueryString["Id"] != null)

{

if (ViewState["action"] == "editing")

{

(..)

}

else

{

(..)

}

}



Any help would be appriciated!

Greets,

Gab
 
Back
Top