casting problem

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hey all,
the statement below is giving me a problem and i'm not sure why:
medDrp.SelectedIndex = (ViewState["test"] == null) ? -1 : ViewState["test"];

the message is saying that i can't implicitly convert an object to int, Huh?

thanks,
rodchar
 
It doesn't trust you that ViewState["test"] is an int - try

medDrp.SelectedIndex = ViewState["test"] == null ? -1 : (int)
ViewState["test"];

Marc
 
rodchar said:
hey all,
the statement below is giving me a problem and i'm not sure why:
medDrp.SelectedIndex = (ViewState["test"] == null) ? -1 :
ViewState["test"];

the message is saying that i can't implicitly convert an object to int,
Huh?

thanks,
rodchar

Hi rodchar,

You may be interested to know that you can compact that statement (if you're
using in C# 2.0) down to:

///
medDrp.SelectedIndex = (int) (ViewState["test"] ?? -1);
///

Where ?? is the null coalessence operator, and tests for nullity on the
first component, returning the second component if null and simply the
first if not.
 
Awesome, thank you everyone again for this help.

Tom Spink said:
rodchar said:
hey all,
the statement below is giving me a problem and i'm not sure why:
medDrp.SelectedIndex = (ViewState["test"] == null) ? -1 :
ViewState["test"];

the message is saying that i can't implicitly convert an object to int,
Huh?

thanks,
rodchar

Hi rodchar,

You may be interested to know that you can compact that statement (if you're
using in C# 2.0) down to:

///
medDrp.SelectedIndex = (int) (ViewState["test"] ?? -1);
///

Where ?? is the null coalessence operator, and tests for nullity on the
first component, returning the second component if null and simply the
first if not.

--
Hope this helps,
Tom Spink

Google first, ask later.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top