hi,
from the SDK:
Colors myOrange = (Colors)Enum.Parse(typeof(Colors), "Red, Yellow");
i completely understand the semantics of this statement, and it is
obviously a very complete way of specifying the cast. but it is clunky
because of the double cast (the above code is telling the compiler
twice that we are working with the 'Color' enum). i tried explaining
this to a novice programmer and i ended up saying "just live with it,
it's an oddity of the framework".
my preferred syntax would be to just do a direct cast without using
Enum.Parse. will this be a feature of future .net compilers? i know
its early days to be talking about .net 3 but why not. i think it is a
natural evolvement of higher level code that is easier to write and
read.
tim
Rick Strahl [MVP] - 25 Jan 2006 09:52 GMT
Without type inferrance you will need the double cast. C# 3.0 will let you
do this without the explicit outer cast using the var statement which infers
type based on the return value of an operation IF that type can be inferred.
Now I think in the case of Enum.Parse that's still not going to help because
the return value is in fact object.
Something has to tell the runtime what type is used...
+++ Rick ---

Signature
Rick Strahl
West Wind Technologies
www.west-wind.com
www.west-wind.com/weblog
> hi,
> from the SDK:
[quoted text clipped - 15 lines]
>
> tim
Tim_Mac - 25 Jan 2006 10:11 GMT
i wonder could they borrow some cleverness from the Templates used in
the Collections.Generic classes. it may not work that way though.
i understand that Enum.Parse returns an object, and it will probably
stay that way. but it would be nice if the compiler would promote the
returned value. that could be seen as 'loose' programming though, a
bit like what you could get away with in VB6.
Lasse Vågsæther Karlsen - 25 Jan 2006 11:59 GMT
> hi,
> from the SDK:
>
> Colors myOrange = (Colors)Enum.Parse(typeof(Colors), "Red, Yellow");
<snip>
> my preferred syntax would be to just do a direct cast without using
> Enum.Parse. will this be a feature of future .net compilers? i know
> its early days to be talking about .net 3 but why not. i think it is a
<snip>
A future generic version might allow us to do:
Colors myOrange = Enum.Parse<Colors>("Red, Yellow");
or with the new type inference in .NET 3.0/C# 3.0:
var myOrange = Enum.Parse<Colors>("Red, Yellow");
But no such thing yet.

Signature
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:lasse@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Tim_Mac - 25 Jan 2006 14:37 GMT
i like the first option. that is interesting to know that such a thing
might be possible. i hadn't even heard of any 3.0 development yet.
thanks for the info.
tim
Dustin Campbell - 25 Jan 2006 16:10 GMT
> i hadn't even heard of any 3.0 development yet.
Check out this link for info: http://msdn.microsoft.com/netframework/future/
-----
Best Regards,
Dustin Campbell
Developer Express Inc
Holger Grund - 25 Jan 2006 20:02 GMT
>i like the first option. that is interesting to know that such a thing
> might be possible. i hadn't even heard of any 3.0 development yet.
There is nothing that prevents you from writing such a function yourself
today.
Something like (C++/CLI that is)
generic < typename EnumT > static EnumT Parse( String^ s ) {
return safe_cast<EnumT> ( Enum::Parse( EnumT::typeid, s ) );
}
Which probably translates to (I'm afraid my C# skills are fairly limited)
class EnumParser {
public static EnumT Parse<EnumT> ( string s ) {
return (EnumT)Enum.Parse( typeof(EnumT), s );
}
};