Question about casting datatypes

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

    #1

    Question about casting datatypes

    hy = new System.Web.UI.W ebControls.Hype rLink();

    Why this doesn't work?

    if(something == true)
    hy.NavigateUrl = "email.aspx?id= " + (string) p+1; ****



    but this works correctly?

    if(something == true)
    hy.NavigateUrl = "email.aspx?id= " + Convert.ToStrin g(p+1);

    **** this way I got the following error:
    Cannot convert type 'int' to 'string'

    Why? (string) shouldn't cast p+1 result to a string?

    TIA
  • J

    #2
    Re: Question about casting datatypes

    Cast and Convert.ToStrin g are 2 different concepts. Cast is not a
    conversion routine.

    "valmir cinquini" <vcinquini@ig.c om.br> wrote in message
    news:ca07badb.0 310061255.2471a 76e@posting.goo gle.com...[color=blue]
    > hy = new System.Web.UI.W ebControls.Hype rLink();
    >
    > Why this doesn't work?
    >
    > if(something == true)
    > hy.NavigateUrl = "email.aspx?id= " + (string) p+1; ****
    >
    >
    >
    > but this works correctly?
    >
    > if(something == true)
    > hy.NavigateUrl = "email.aspx?id= " + Convert.ToStrin g(p+1);
    >
    > **** this way I got the following error:
    > Cannot convert type 'int' to 'string'
    >
    > Why? (string) shouldn't cast p+1 result to a string?
    >
    > TIA[/color]


    Comment

    • Ignacio Machin \( .NET/ C#  MVP \)

      #3
      Re: Question about casting datatypes

      Hi,

      It's very simple,
      (string)p + 1 means:

      1)- treat p as string
      2)- to the result of the above operation add the numeric value 1

      Now this will ALWAYS FAIL because:
      if p is numeric 1) will give error, as you cannot cast a numeric value to
      string.
      if p is convertible to string, then the error will be in 2) as 1 is numeric
      and you cannot sum a numeric and a string value

      The only way to do this is using convertion. now there is two possible ways
      of doing so:
      1) Convert.ToStrin g( p + 1)
      2- (p+1).ToString( )


      I would STRONGLY recommend 1 as it's clearer.

      Hope this help,

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


      "valmir cinquini" <vcinquini@ig.c om.br> wrote in message
      news:ca07badb.0 310061255.2471a 76e@posting.goo gle.com...[color=blue]
      > hy = new System.Web.UI.W ebControls.Hype rLink();
      >
      > Why this doesn't work?
      >
      > if(something == true)
      > hy.NavigateUrl = "email.aspx?id= " + (string) p+1; ****
      >
      >
      >
      > but this works correctly?
      >
      > if(something == true)
      > hy.NavigateUrl = "email.aspx?id= " + Convert.ToStrin g(p+1);
      >
      > **** this way I got the following error:
      > Cannot convert type 'int' to 'string'
      >
      > Why? (string) shouldn't cast p+1 result to a string?
      >
      > TIA[/color]


      Comment

      • valmir cinquini

        #4
        Re: Question about casting datatypes

        "J" <j> wrote in message news:<vo3muu1p3 gsi70@corp.supe rnews.com>...[color=blue]
        > Cast and Convert.ToStrin g are 2 different concepts. Cast is not a
        > conversion routine.[/color]

        Ok. I understand it, but p is an integer variable. Why can't I convert
        it in a string using an cast operation?

        Is it because I'm trying concatenate the convertion result with the
        string "email.aspx?id= "?

        p originates from this line below:

        int p = int.Parse(idNew s);

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: Question about casting datatypes

          valmir cinquini <vcinquini@ig.c om.br> wrote:[color=blue]
          > "J" <j> wrote in message news:<vo3muu1p3 gsi70@corp.supe rnews.com>...[color=green]
          > > Cast and Convert.ToStrin g are 2 different concepts. Cast is not a
          > > conversion routine.[/color]
          >
          > Ok. I understand it, but p is an integer variable. Why can't I convert
          > it in a string using an cast operation?[/color]

          Because an integer just isn't a string, and there's no explicit
          conversion *operator* from string to integer. Casts are there either to
          tell the compiler to insert a runtime check that a reference to (say)
          Object is really a reference to a String, or to tell it to invoke an
          explicit conversion operator from one type to another.
          [color=blue]
          > Is it because I'm trying concatenate the convertion result with the
          > string "email.aspx?id= "?[/color]

          No.

          --
          Jon Skeet - <skeet@pobox.co m>
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

          If replying to the group, please do not mail me too

          Comment

          • valmir cinquini

            #6
            Re: Question about casting datatypes

            John and Ignacio

            Thanks for the explanations, they clear this up for me.

            I learned C for DOS too many years ago and after that I always
            developed in VB6. Since I'm living this .Net world, sometimes I've
            caugh myself thinking as if I was 20 year old...good times, good
            times...

            For short, I cannot cast int to string because int is a native
            datatype and string is a Object. Or not? Am I still wrong?


            "Ignacio Machin \( .NET/ C# MVP \)" <ignacio.mach in AT dot.state.fl.us > wrote in message news:<uFgU0ZNjD HA.4036@tk2msft ngp13.phx.gbl>. ..[color=blue]
            > Hi,
            >
            > It's very simple,
            > (string)p + 1 means:
            >
            > 1)- treat p as string
            > 2)- to the result of the above operation add the numeric value 1
            >
            > Now this will ALWAYS FAIL because:
            > if p is numeric 1) will give error, as you cannot cast a numeric value to
            > string.
            > if p is convertible to string, then the error will be in 2) as 1 is numeric
            > and you cannot sum a numeric and a string value
            >
            > The only way to do this is using convertion. now there is two possible ways
            > of doing so:
            > 1) Convert.ToStrin g( p + 1)
            > 2- (p+1).ToString( )
            >
            >
            > I would STRONGLY recommend 1 as it's clearer.
            >
            > Hope this help,
            >
            > --
            > Ignacio Machin,
            > ignacio.machin AT dot.state.fl.us
            > Florida Department Of Transportation
            >
            >
            > "valmir cinquini" <vcinquini@ig.c om.br> wrote in message
            > news:ca07badb.0 310061255.2471a 76e@posting.goo gle.com...[color=green]
            > > hy = new System.Web.UI.W ebControls.Hype rLink();
            > >
            > > Why this doesn't work?
            > >
            > > if(something == true)
            > > hy.NavigateUrl = "email.aspx?id= " + (string) p+1; ****
            > >
            > >
            > >
            > > but this works correctly?
            > >
            > > if(something == true)
            > > hy.NavigateUrl = "email.aspx?id= " + Convert.ToStrin g(p+1);
            > >
            > > **** this way I got the following error:
            > > Cannot convert type 'int' to 'string'
            > >
            > > Why? (string) shouldn't cast p+1 result to a string?
            > >
            > > TIA[/color][/color]

            Comment

            Working...