Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Sigh... Stupid Generics and Casting...

Sigh... Stupid Generics and Casting...

Scheduled Pinned Locked Moved The Lounge
htmldatabasecomsysadminquestion
15 Posts 9 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B BillWoodruff

    Well, congratulations, Zac; I do think this type of post should go on the C# forum: you might get some interesting other ideas. Hope you do post a tip/trick ! best, Bill

    "One of the few good things about modern times: If you die horribly on television, you will not have died in vain. You will have entertained us." Kurt Vonnegut

    B Offline
    B Offline
    Brisingr Aerowing
    wrote on last edited by
    #4

    Thanks, I will!

    Bill Gates is a very rich man today... and do you want to know why? The answer is one word: versions. Dave Barry Read more at [BrainyQuote](http://www.brainyquote.com/quotes/topics topic_technology.html#yAfSEbrfumitrteO.99)[^]

    1 Reply Last reply
    0
    • P PIEBALDconsult

      Zac Greve wrote:

      Convert.ChangeType(Object, Type)

      :thumbsup: The only useful member of Convert.

      B Offline
      B Offline
      Brisingr Aerowing
      wrote on last edited by
      #5

      Yeah, I was looking at the convert class in the documentation, and then it dawned on me.

      Bill Gates is a very rich man today... and do you want to know why? The answer is one word: versions. Dave Barry Read more at [BrainyQuote](http://www.brainyquote.com/quotes/topics topic_technology.html#yAfSEbrfumitrteO.99)[^]

      1 Reply Last reply
      0
      • B Brisingr Aerowing

        Well, I have spent about four hours trying to cast one generic type to another. I have been doing this for one of my projects, which allows multiple users and stores data in a database on a server. I have been using a dictionary to store the settings, and have the key and value as strings (I know that that will serialize), and have created two extension methods to get values and return a specified default if the key doesn't exist. The first returns the value as the type of the dictionary's value type, and the second casts it to a different type, returning the default if the cast fails. I finally was able to get it to work by using Convert.ChangeType(Object, Type) and casting the result to the specified return type. I will post a tip/trick if you guys want.

        Bill Gates is a very rich man today... and do you want to know why? The answer is one word: versions. Dave Barry Read more at [BrainyQuote](http://www.brainyquote.com/quotes/topics topic_technology.html#yAfSEbrfumitrteO.99)[^]

        C Offline
        C Offline
        Clifford Nelson
        wrote on last edited by
        #6

        If worse comes to worse there is always the dynamic type.

        B 1 Reply Last reply
        0
        • B Brisingr Aerowing

          Well, I have spent about four hours trying to cast one generic type to another. I have been doing this for one of my projects, which allows multiple users and stores data in a database on a server. I have been using a dictionary to store the settings, and have the key and value as strings (I know that that will serialize), and have created two extension methods to get values and return a specified default if the key doesn't exist. The first returns the value as the type of the dictionary's value type, and the second casts it to a different type, returning the default if the cast fails. I finally was able to get it to work by using Convert.ChangeType(Object, Type) and casting the result to the specified return type. I will post a tip/trick if you guys want.

          Bill Gates is a very rich man today... and do you want to know why? The answer is one word: versions. Dave Barry Read more at [BrainyQuote](http://www.brainyquote.com/quotes/topics topic_technology.html#yAfSEbrfumitrteO.99)[^]

          E Offline
          E Offline
          Ennis Ray Lynch Jr
          wrote on last edited by
          #7

          PITA, huh. I have stripped out generics in some places because of that "issue"

          Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

          1 Reply Last reply
          0
          • C Clifford Nelson

            If worse comes to worse there is always the dynamic type.

            B Offline
            B Offline
            Brisingr Aerowing
            wrote on last edited by
            #8

            In C#, not VB. And yes, I always have option strict on and never use goto! People who use that should go to some other line of work!

            Bill Gates is a very rich man today... and do you want to know why? The answer is one word: versions. Dave Barry Read more at [BrainyQuote](http://www.brainyquote.com/quotes/topics topic_technology.html#yAfSEbrfumitrteO.99)[^]

            A 1 Reply Last reply
            0
            • B Brisingr Aerowing

              In C#, not VB. And yes, I always have option strict on and never use goto! People who use that should go to some other line of work!

              Bill Gates is a very rich man today... and do you want to know why? The answer is one word: versions. Dave Barry Read more at [BrainyQuote](http://www.brainyquote.com/quotes/topics topic_technology.html#yAfSEbrfumitrteO.99)[^]

              A Offline
              A Offline
              AspDotNetDev
              wrote on last edited by
              #9

              Dynamic objects aren't what you are thinking. See here. Also, I'm not exactly sure what you are doing, but covariance and contravariance may be of use, though I don't understand them well myself.

              Thou mewling ill-breeding pignut!

              B 1 Reply Last reply
              0
              • A AspDotNetDev

                Dynamic objects aren't what you are thinking. See here. Also, I'm not exactly sure what you are doing, but covariance and contravariance may be of use, though I don't understand them well myself.

                Thou mewling ill-breeding pignut!

                B Offline
                B Offline
                Brisingr Aerowing
                wrote on last edited by
                #10

                I don't see anything about VB there. I think VB 12 (.NET 4.5) has introduced it, but I am not certain.

                Bill Gates is a very rich man today... and do you want to know why? The answer is one word: versions. Dave Barry Read more at [BrainyQuote](http://www.brainyquote.com/quotes/topics topic_technology.html#yAfSEbrfumitrteO.99)[^]

                A 1 Reply Last reply
                0
                • B Brisingr Aerowing

                  I don't see anything about VB there. I think VB 12 (.NET 4.5) has introduced it, but I am not certain.

                  Bill Gates is a very rich man today... and do you want to know why? The answer is one word: versions. Dave Barry Read more at [BrainyQuote](http://www.brainyquote.com/quotes/topics topic_technology.html#yAfSEbrfumitrteO.99)[^]

                  A Offline
                  A Offline
                  AspDotNetDev
                  wrote on last edited by
                  #11

                  Gotcha. I thought you meant that you were working in C#.

                  Thou mewling ill-breeding pignut!

                  B 1 Reply Last reply
                  0
                  • A AspDotNetDev

                    Gotcha. I thought you meant that you were working in C#.

                    Thou mewling ill-breeding pignut!

                    B Offline
                    B Offline
                    Brisingr Aerowing
                    wrote on last edited by
                    #12

                    I usually use VB, but I know C# as well, and use it if I need to (e.g. work with a 3rd party library/code files/etc.). I can also translate C# to VB and VB to C#, but use online converters (mostly Telerik Code Converter[^]) to convert large files.

                    Bill Gates is a very rich man today... and do you want to know why? The answer is one word: versions. Dave Barry Read more at [BrainyQuote](http://www.brainyquote.com/quotes/topics topic_technology.html#yAfSEbrfumitrteO.99)[^]

                    1 Reply Last reply
                    0
                    • B Brisingr Aerowing

                      Well, I have spent about four hours trying to cast one generic type to another. I have been doing this for one of my projects, which allows multiple users and stores data in a database on a server. I have been using a dictionary to store the settings, and have the key and value as strings (I know that that will serialize), and have created two extension methods to get values and return a specified default if the key doesn't exist. The first returns the value as the type of the dictionary's value type, and the second casts it to a different type, returning the default if the cast fails. I finally was able to get it to work by using Convert.ChangeType(Object, Type) and casting the result to the specified return type. I will post a tip/trick if you guys want.

                      Bill Gates is a very rich man today... and do you want to know why? The answer is one word: versions. Dave Barry Read more at [BrainyQuote](http://www.brainyquote.com/quotes/topics topic_technology.html#yAfSEbrfumitrteO.99)[^]

                      J Offline
                      J Offline
                      Jonathan C Dickinson
                      wrote on last edited by
                      #13

                      You can also just round-trip it via object, in C# terms (can't remember the VB, been 10 years since I used it):

                      var casted = (T)(object)someValue;

                      Assuming that someValue can actually be casted (as opposed to converted) to T.

                      He who asks a question is a fool for five minutes. He who does not ask a question remains a fool forever. [Chineese Proverb] Jonathan C Dickinson (C# Software Engineer)

                      B 1 Reply Last reply
                      0
                      • J Jonathan C Dickinson

                        You can also just round-trip it via object, in C# terms (can't remember the VB, been 10 years since I used it):

                        var casted = (T)(object)someValue;

                        Assuming that someValue can actually be casted (as opposed to converted) to T.

                        He who asks a question is a fool for five minutes. He who does not ask a question remains a fool forever. [Chineese Proverb] Jonathan C Dickinson (C# Software Engineer)

                        B Offline
                        B Offline
                        BobJanova
                        wrote on last edited by
                        #14

                        If the cast can be done, then you don't need object in there:

                        void SomeMethod<T>(List<T> list){
                        foreach(T item in list)
                        DoSomethingWith((U)item);
                        }

                        ... will work as long as T is castable to U.

                        D 1 Reply Last reply
                        0
                        • B BobJanova

                          If the cast can be done, then you don't need object in there:

                          void SomeMethod<T>(List<T> list){
                          foreach(T item in list)
                          DoSomethingWith((U)item);
                          }

                          ... will work as long as T is castable to U.

                          D Offline
                          D Offline
                          Daniel Grunwald
                          wrote on last edited by
                          #15

                          No, that will fail to compile unless T and U are known to be related at compile-time. See §6.2.7 "Explicit conversions involving type parameters" in the C# specification for an explanation.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups