Cast from name syntax

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

    #1

    Cast from name syntax

    Hi all,

    I am trying to cast a Double (its actually the NextDouble method of the
    Random class, but could be any type of number) to a Type via its name.

    Im trying the following syntax:

    ThisNumber = (Type.GetType(" MyType.Decimal" ))(ThisRandom.N extDouble());

    Im getting an error stating that 'Method name expected', so either im
    not casting correctly or using incorrect syntax?

    Thanks,

    Nick

  • Stoitcho Goutsev \(100\) [C# MVP]

    #2
    Re: Cast from name syntax

    Nick,

    when you use type casting using the syntax: "(<type>) obj" or "obj as
    <type>", the <type> name has to be know during compilation. In other word
    you can use (int)var, but cannot use (typeof(int))va r or something like
    that.

    BTW in your code sample you do know the type you want to covert to it is
    MyType.Decimal. Why don't you use

    ThisNumber = (MyType.Decimal )(ThisRandom.Ne xtDouble()); this will compile as
    long as there is conversion between double and MyType.Decimal.

    There is no general conversion from any type to any type. Usually there are
    two types of dynamic conversion. One is when a type implements IConvertible
    and the other is when a type has TypeConverter attached to it via an
    attribute.

    Since you want to convert from double to your own type IConvertible won't
    work for you. Thus what you might wanna do is to create a TypeConverter for
    your own type.

    However, look again your design. I've found that in most of the cases when
    programmers think that they don't know the type they want to convert to at
    compile time, this is simply not the case. Look again the code you've
    posted. I know that this is just a sample and the real world scenario could
    be more complex.



    Stoitcho Goutsev (100) [C# MVP]

    "Nick Weekes" <nickjunkinbox@ yahoo.co.uk> wrote in message
    news:1118319575 .412197.138500@ g49g2000cwa.goo glegroups.com.. .[color=blue]
    > Hi all,
    >
    > I am trying to cast a Double (its actually the NextDouble method of the
    > Random class, but could be any type of number) to a Type via its name.
    >
    > Im trying the following syntax:
    >
    > ThisNumber = (Type.GetType(" MyType.Decimal" ))(ThisRandom.N extDouble());
    >
    > Im getting an error stating that 'Method name expected', so either im
    > not casting correctly or using incorrect syntax?
    >
    > Thanks,
    >
    > Nick
    >[/color]


    Comment

    • Nick Weekes

      #3
      Re: Cast from name syntax

      Stoitcho,

      Thanks for the explanation. Why is it not possible to cast an unknown,
      for example (typeof(MyType) )var?

      Cheers,

      Nick

      Comment

      • Jonathan Allen

        #4
        Re: Cast from name syntax

        First the difference between casting and converting.

        When you are casting, you are saying that you know object X is really of
        type Y. You are in effect telling the complier that it is safe to assign
        object X to variable Z. There is no change to the underlying object.

        Y Z = (Y)X;

        When you are converting, you are creating a new object that has the same
        value as the old one, but is in a different format. For example, when making
        an integer into a double.
        [color=blue]
        > Thanks for the explanation. Why is it not possible to cast an unknown,
        > for example (typeof(MyType) )var?[/color]

        You aren't really casting in the design sense because you cannot make that
        promise. Besides, when would you not know what type you wanted to cast to?

        (You are still casting in the runtime sense. That is, the runtime is
        verifying the cast is safe and the types are equivalent.)

        --
        Jonathan Allen


        "Nick Weekes" <nickjunkinbox@ yahoo.co.uk> wrote in message
        news:1118332836 .222986.156880@ g44g2000cwa.goo glegroups.com.. .[color=blue]
        > Stoitcho,
        >
        > Thanks for the explanation. Why is it not possible to cast an unknown,
        > for example (typeof(MyType) )var?
        >
        > Cheers,
        >
        > Nick
        >[/color]


        Comment

        • Jonathan Allen

          #5
          Re: Cast from name syntax

          First the difference between casting and converting.

          When you are casting, you are saying that you know object X is really of
          type Y. You are in effect telling the complier that it is safe to assign
          object X to variable Z. There is no change to the underlying object.

          Y Z = (Y)X;

          When you are converting, you are creating a new object that has the same
          value as the old one, but is in a different format. For example, when making
          an integer into a double.
          [color=blue]
          > Thanks for the explanation. Why is it not possible to cast an unknown,
          > for example (typeof(MyType) )var?[/color]

          You aren't really casting in the design sense because you cannot make that
          promise. Besides, when would you not know what type you wanted to cast to?

          (You are still casting in the runtime sense. That is, the runtime is
          verifying the cast is safe and the types are equivalent.)

          --
          Jonathan Allen


          "Nick Weekes" <nickjunkinbox@ yahoo.co.uk> wrote in message
          news:1118332836 .222986.156880@ g44g2000cwa.goo glegroups.com.. .[color=blue]
          > Stoitcho,
          >
          > Thanks for the explanation. Why is it not possible to cast an unknown,
          > for example (typeof(MyType) )var?
          >
          > Cheers,
          >
          > Nick
          >[/color]


          Comment

          • Stoitcho Goutsev \(100\) [C# MVP]

            #6
            Re: Cast from name syntax

            This is just a language syntax, but I guess the reason is that the IL
            instruction that this expression emits expects a type metadata token. It
            cannot be generated at compile type if you use a variable that doesn't have
            value at compile time. Keep in mind that the IL instruction for conversion
            *castclass* converts only to/from classes and itnerfaces that are in
            base/derived class relationship or the interface is implemented by the type.
            All other convrsion techniques like IConvertible, TypeConverter or type
            casting operators overloading (C#) is not taken into account. All such
            conversions has to be done explicitly by the progeammer or the compiler.
            They are not part of the .NET Runtime so to speak.

            Dynamic conversion cannot be provided out of the box. The one who writes the
            class only know how to convert it to/from another type. For example how
            could the runtyme know how to cnonvert from string to Foo. Only me (the
            programmer who wrote this foo class) knows how to do that and if this
            conversion makes any sense after all. The framework provides an
            infrastructure for dynamic conversion
            IConvertible and Convert class, or TypeConverter, but the implementation is
            up to the developer.

            The sample that I gave
            (typeof(MyType) )var?

            doesn't make much sense because this line can be re-written simply to
            (MyType)var and it will compile.

            but this (what actually I wanted to demonstrate) is not possible

            Type t = var1.GetType();
            var1 = (t)var2; // at compile tyme t doesn't have value.


            --
            Stoitcho Goutsev (100) [C# MVP]



            "Nick Weekes" <nickjunkinbox@ yahoo.co.uk> wrote in message
            news:1118332836 .222986.156880@ g44g2000cwa.goo glegroups.com.. .[color=blue]
            > Stoitcho,
            >
            > Thanks for the explanation. Why is it not possible to cast an unknown,
            > for example (typeof(MyType) )var?
            >
            > Cheers,
            >
            > Nick
            >[/color]


            Comment

            • Stoitcho Goutsev \(100\) [C# MVP]

              #7
              Re: Cast from name syntax

              This is just a language syntax, but I guess the reason is that the IL
              instruction that this expression emits expects a type metadata token. It
              cannot be generated at compile type if you use a variable that doesn't have
              value at compile time. Keep in mind that the IL instruction for conversion
              *castclass* converts only to/from classes and itnerfaces that are in
              base/derived class relationship or the interface is implemented by the type.
              All other convrsion techniques like IConvertible, TypeConverter or type
              casting operators overloading (C#) is not taken into account. All such
              conversions has to be done explicitly by the progeammer or the compiler.
              They are not part of the .NET Runtime so to speak.

              Dynamic conversion cannot be provided out of the box. The one who writes the
              class only know how to convert it to/from another type. For example how
              could the runtyme know how to cnonvert from string to Foo. Only me (the
              programmer who wrote this foo class) knows how to do that and if this
              conversion makes any sense after all. The framework provides an
              infrastructure for dynamic conversion
              IConvertible and Convert class, or TypeConverter, but the implementation is
              up to the developer.

              The sample that I gave
              (typeof(MyType) )var?

              doesn't make much sense because this line can be re-written simply to
              (MyType)var and it will compile.

              but this (what actually I wanted to demonstrate) is not possible

              Type t = var1.GetType();
              var1 = (t)var2; // at compile tyme t doesn't have value.


              --
              Stoitcho Goutsev (100) [C# MVP]



              "Nick Weekes" <nickjunkinbox@ yahoo.co.uk> wrote in message
              news:1118332836 .222986.156880@ g44g2000cwa.goo glegroups.com.. .[color=blue]
              > Stoitcho,
              >
              > Thanks for the explanation. Why is it not possible to cast an unknown,
              > for example (typeof(MyType) )var?
              >
              > Cheers,
              >
              > Nick
              >[/color]


              Comment

              Working...