Dynamic Typecasting

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?aG9udXM=?=

    #1

    Dynamic Typecasting

    I have a variable and a data-type string stored in a database. I need to
    dynamically cast the variable to the datatype at run-time. I am able to
    create the Type from the data-type string, but I am not able to cast it
    correctly. Here is an example of my code, although realize that I would need
    to accomplish this with any type (Boolean, Guid, String, Integer...), and not
    just GUID:

    Dim typeStr as String = "System.GUI D"
    Dim myType As Type = Type.GetType(ty peStr, True, True)
    Dim varStr as String = "81db359d-a970-4247-89f8-67995412da35"

    'this is the code that does not work. myType gets underlined in blue,
    saying "Type 'tmpType' is not defined."
    varStr = CType(varStr, myType)


    --
    I am living proof that 95% of programmers are idiots.
  • Cor Ligthert[MVP]

    #2
    Re: Dynamic Typecasting

    Hi Honus,

    Why you want to do it this, way, it sounds to me as.

    I need a billion dollars tomorrow, can you tell me how to do that?

    Cor

    "honus" <honus@discussi ons.microsoft.c omschreef in bericht
    news:B74829CD-D8B8-4BA8-929B-26382F34D58A@mi crosoft.com...
    >I have a variable and a data-type string stored in a database. I need to
    dynamically cast the variable to the datatype at run-time. I am able to
    create the Type from the data-type string, but I am not able to cast it
    correctly. Here is an example of my code, although realize that I would
    need
    to accomplish this with any type (Boolean, Guid, String, Integer...), and
    not
    just GUID:
    >
    Dim typeStr as String = "System.GUI D"
    Dim myType As Type = Type.GetType(ty peStr, True, True)
    Dim varStr as String = "81db359d-a970-4247-89f8-67995412da35"
    >
    'this is the code that does not work. myType gets underlined in blue,
    saying "Type 'tmpType' is not defined."
    varStr = CType(varStr, myType)
    >
    >
    --
    I am living proof that 95% of programmers are idiots.

    Comment

    • Armin Zingler

      #3
      Re: Dynamic Typecasting

      "honus" <honus@discussi ons.microsoft.c omschrieb
      I have a variable and a data-type string stored in a database. I
      need to dynamically cast the variable to the datatype at run-time.
      I am able to create the Type from the data-type string, but I am not
      able to cast it correctly. Here is an example of my code, although
      realize that I would need to accomplish this with any type (Boolean,
      Guid, String, Integer...), and not just GUID:
      >
      Dim typeStr as String = "System.GUI D"
      Dim myType As Type = Type.GetType(ty peStr, True, True)
      Dim varStr as String = "81db359d-a970-4247-89f8-67995412da35"
      >
      'this is the code that does not work. myType gets underlined in
      blue, saying "Type 'tmpType' is not defined."
      varStr = CType(varStr, myType)
      What you want to do is contradictive:
      - Casting is done by the compiler. The compiler must know the destination
      type.
      - Your destination type is unknown at compile time, therefore it can not
      work.


      Armin

      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: Dynamic Typecasting

        "honus" <honus@discussi ons.microsoft.c omschrieb:
        >I have a variable and a data-type string stored in a database. I need to
        dynamically cast the variable to the datatype at run-time.
        Casting is primarily a compile-time feature which will help the compiler to
        detect certain flaws in the code.

        If you are talking about type conversions, take a look at the
        'TypeConverter' class.

        --
        M S Herfried K. Wagner
        M V P <URL:http://dotnet.mvps.org/>
        V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

        Comment

        • =?Utf-8?B?aG9udXM=?=

          #5
          Re: Dynamic Typecasting

          Your synnicism is refreshing and envigorating. Oh wait, never mind.

          "Cor Ligthert[MVP]" wrote:
          Hi Honus,
          >
          Why you want to do it this, way, it sounds to me as.
          >
          I need a billion dollars tomorrow, can you tell me how to do that?
          >
          Cor
          >
          "honus" <honus@discussi ons.microsoft.c omschreef in bericht
          news:B74829CD-D8B8-4BA8-929B-26382F34D58A@mi crosoft.com...
          I have a variable and a data-type string stored in a database. I need to
          dynamically cast the variable to the datatype at run-time. I am able to
          create the Type from the data-type string, but I am not able to cast it
          correctly. Here is an example of my code, although realize that I would
          need
          to accomplish this with any type (Boolean, Guid, String, Integer...), and
          not
          just GUID:

          Dim typeStr as String = "System.GUI D"
          Dim myType As Type = Type.GetType(ty peStr, True, True)
          Dim varStr as String = "81db359d-a970-4247-89f8-67995412da35"

          'this is the code that does not work. myType gets underlined in blue,
          saying "Type 'tmpType' is not defined."
          varStr = CType(varStr, myType)


          --
          I am living proof that 95% of programmers are idiots.
          >
          >

          Comment

          • Cor Ligthert[MVP]

            #6
            Re: Dynamic Typecasting

            Honus,
            Your synnicism is refreshing and envigorating. Oh wait, never mind.
            >
            What you call this what you are now writing, informing us in a way that we
            able to help you?

            We are sharing information here, that is not in one direction, we try to
            learn from each other.

            Cor

            Comment

            • =?Utf-8?B?TW9kZWxCdWlsZGVy?=

              #7
              RE: Dynamic Typecasting

              How is an object of type List (of String) serialized into the database?

              "honus" wrote:
              I have a variable and a data-type string stored in a database. I need to
              dynamically cast the variable to the datatype at run-time. I am able to
              create the Type from the data-type string, but I am not able to cast it
              correctly. Here is an example of my code, although realize that I would need
              to accomplish this with any type (Boolean, Guid, String, Integer...), and not
              just GUID:
              >
              Dim typeStr as String = "System.GUI D"
              Dim myType As Type = Type.GetType(ty peStr, True, True)
              Dim varStr as String = "81db359d-a970-4247-89f8-67995412da35"
              >
              'this is the code that does not work. myType gets underlined in blue,
              saying "Type 'tmpType' is not defined."
              varStr = CType(varStr, myType)
              >
              >
              --
              I am living proof that 95% of programmers are idiots.

              Comment

              • Phill W.

                #8
                Re: Dynamic Typecasting

                honus wrote:
                I have a variable and a data-type string stored in a database. I need to
                dynamically cast the variable to the datatype at run-time. I am able to
                create the Type from the data-type string, but I am not able to cast it
                correctly.
                Dim typeStr as String = "System.GUI D"
                Dim myType As Type = Type.GetType(ty peStr, True, True)
                Dim varStr as String = "81db359d-a970-4247-89f8-67995412da35"
                All the data types have to be known at compile type, unless you resort
                to some /really/ nasty P/Invoke-style coding (Yuk!).

                Rather than that, since you will have the Type available at runtime, you
                can use Activator.Creat eInstance() to instantiate it:

                Dim o As Object _
                = Activator.Creat eInstance( myType )

                But, of course, "as Object" is /nowhere/ as useful "as Variant" used to
                be... :-)

                HTH,
                Phill W.

                Comment

                • Patrice

                  #9
                  Re: Dynamic Typecasting

                  If not yet solved your best bet is likely to explain what you are trying to
                  do as the code fragment you show doesn't make really sense (for example
                  casting a variable VarStr to another type and then affecting this variable
                  to the same variable doesn't make really sense).

                  You can create a guid using Dim g as new Guid(VarStr) and you can convert
                  the GUID back to text using the ToString method.
                  ---
                  Patrice

                  "honus" <honus@discussi ons.microsoft.c oma écrit dans le message de news:
                  B74829CD-D8B8-4BA8-929B-26382F34D58A@mi crosoft.com...
                  >I have a variable and a data-type string stored in a database. I need to
                  dynamically cast the variable to the datatype at run-time. I am able to
                  create the Type from the data-type string, but I am not able to cast it
                  correctly. Here is an example of my code, although realize that I would
                  need
                  to accomplish this with any type (Boolean, Guid, String, Integer...), and
                  not
                  just GUID:
                  >
                  Dim typeStr as String = "System.GUI D"
                  Dim myType As Type = Type.GetType(ty peStr, True, True)
                  Dim varStr as String = "81db359d-a970-4247-89f8-67995412da35"
                  >
                  'this is the code that does not work. myType gets underlined in blue,
                  saying "Type 'tmpType' is not defined."
                  varStr = CType(varStr, myType)
                  >
                  >
                  --
                  I am living proof that 95% of programmers are idiots.

                  Comment

                  Working...