C# Conversion Differences

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

    C# Conversion Differences

    Why will this fail:

    (short) txtQty.Text

    and this succeed?

    Convert.ToInt16 (txtQty.Text)


  • Jon Skeet [C# MVP]

    #2
    Re: C# Conversion Differences

    Scott M. <NoSpam@NoSpam. comwrote:
    Why will this fail:
    >
    (short) txtQty.Text
    That's trying to do a *cast* from a string to a short - and no such
    *language* conversion is available.
    and this succeed?
    >
    Convert.ToInt16 (txtQty.Text)
    That's calling a *method* (rather than casting) which takes a string
    and returns a short.

    --
    Jon Skeet - <skeet@pobox.co m>
    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
    If replying to the group, please do not mail me too

    Comment

    • Arne Vajhøj

      #3
      Re: C# Conversion Differences

      Scott M. wrote:
      Why will this fail:
      >
      (short) txtQty.Text
      >
      and this succeed?
      >
      Convert.ToInt16 (txtQty.Text)
      Two completely different things.

      The second work because Convert.ToInt16 has an
      overload that takes a string.

      The first fails because you can not cast from
      a string to a short.

      Arne

      Comment

      • Bruce Wood

        #4
        Re: C# Conversion Differences


        Scott M. wrote:
        Why will this fail:
        >
        (short) txtQty.Text
        >
        and this succeed?
        >
        Convert.ToInt16 (txtQty.Text)
        Perhaps you misunderstand what a cast is (usually). Conceptually there
        are several kinds of casts in C#. Unfortunately they all use the same
        syntax, so it can be confusing. The kinds of casts that I can think of
        off the top of my head are:

        1. Unboxing casts: you have a value type (such as int) wrapped up to
        look like an Object. When you say

        int x = (int)obj;

        you're trying to unwrap the boxed value and get the integer back again.
        This kind of cast is very restrictive: you have to specify the type in
        the box and nothing else.

        2. Value type coercions: you have one value, such as an int, and you
        want to fit it into another value type, such as a short. When you say

        short s = (short)x;

        you're telling the compiler to convert one numeric type to another, and
        you don't care about any lost precision.

        3. Conversions down the inheritance hierarchy. If you have a class
        Derived that is derived from class Base, and you have:

        Base b = new Derived(...);
        Derived d = (Derived)b;

        what you are saying to the compiler is, "The variable b contains a
        reference to a Derived object (or something else farther down the
        inheritance hierarchy). Trust me. I know."

        4. Finally, you can write casts yourself, which can do anything you
        like. In C# 2.0 and before, you can do this only for your own types,
        but in C# 3.0 I believe that you can use extension methods to add casts
        for existing types.

        The lesson in all of this is that except for the cast mentioned in #4,
        most of the casts in C# don't do anything radical. Conservativsm is the
        watchword. Unlike languages like VB.NET, C# avoids seemingly-simple
        statements that end up doing complex conversions.

        Converting a string to a short is complex. At the very least, it can
        result in format exceptions: what if the string doesn't contain a valid
        representation of a number? What if the user is in Denmark? Egypt?
        China? How does this affect number formatting and how does it affect
        how the machine should interpret the string? It's not as simple as just
        reading the string and pumping out a number.

        For that reason, C# supplies a function to do the conversion. The fact
        that you have to call a function is a hint that this isn't as simple as
        it looks. Notice, in this case, that the method has three overloads: a
        string, a string and a format provider, and a string and an int
        representing the base representation.

        Comment

        • Cor Ligthert [MVP]

          #5
          Re: C# Conversion Differences

          Scott,

          Because you are a long time VB.Net user and contributor to the non C# dotNet
          newsgroups is it in my idea easy trying to explain this from the equalities
          with that language and C#.

          In C# you have as in VB.Net beside the "convert" and "parse" functions the
          same as in VB.Net the CType and DirectCast.

          The "as" keyword in C# gives the same results as the DirectCast in VB.Net.


          The construction between () you show gives in C# the same results as the
          CType in VB.Net.
          (Searching for a link for this one is not easy) but you know it I assume
          from VB.Net. A as you most probably know is this construction not forever a
          direct cast, it can be a convert if the types have user defined conversions,
          the same as the CType in VB.Net)

          I assume that you are well known with the "parse" and "convert" methods,
          knowing how long you are active outside the C# newsgroup.

          Just to give you an idea in another way.

          Cor



          "Scott M." <NoSpam@NoSpam. comschreef in bericht
          news:OADkgTFAHH A.4672@TK2MSFTN GP02.phx.gbl...
          Why will this fail:
          >
          (short) txtQty.Text
          >
          and this succeed?
          >
          Convert.ToInt16 (txtQty.Text)
          >

          Comment

          • Scott M.

            #6
            Re: C# Conversion Differences

            Thanks everyone.


            "Scott M." <NoSpam@NoSpam. comwrote in message
            news:OADkgTFAHH A.4672@TK2MSFTN GP02.phx.gbl...
            Why will this fail:
            >
            (short) txtQty.Text
            >
            and this succeed?
            >
            Convert.ToInt16 (txtQty.Text)
            >

            Comment

            Working...