Sigh... Stupid Generics and Casting...
-
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
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)[^]
-
Zac Greve wrote:
Convert.ChangeType(Object, Type)
:thumbsup: The only useful member of Convert.
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)[^]
-
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)[^]
If worse comes to worse there is always the dynamic type.
-
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)[^]
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
-
If worse comes to worse there is always the dynamic type.
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)[^]
-
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)[^]
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.
-
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.
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)[^]
-
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)[^]
Gotcha. I thought you meant that you were working in C#.
-
Gotcha. I thought you meant that you were working in C#.
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)[^]
-
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)[^]
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) toT
.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)
-
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) toT
.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)
-
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.
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.