casting problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rodchar

    #1

    casting problem

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

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

    thanks,
    rodchar
  • Marc Gravell

    #2
    Re: casting problem

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

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

    Marc


    Comment

    • rodchar

      #3
      Re: casting problem

      thank you very much for the help.

      "Marc Gravell" wrote:
      It doesn't trust you that ViewState["test"] is an int - try
      >
      medDrp.Selected Index = ViewState["test"] == null ? -1 : (int)
      ViewState["test"];
      >
      Marc
      >
      >
      >

      Comment

      • Tom Spink

        #4
        Re: casting problem

        rodchar wrote:
        hey all,
        the statement below is giving me a problem and i'm not sure why:
        medDrp.Selected Index = (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.Selected Index = (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.

        Comment

        • rodchar

          #5
          Re: casting problem

          Awesome, thank you everyone again for this help.

          "Tom Spink" wrote:
          rodchar wrote:
          >
          hey all,
          the statement below is giving me a problem and i'm not sure why:
          medDrp.Selected Index = (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.Selected Index = (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.
          >

          Comment

          Working...