Problem with GetValues on Enum type

  • Thread starter Thread starter Gabriël
  • Start date Start date
G

Gabriël

Hi Folks,

I've tried to do the following:
public enum AssetType { Requested, InOrder, Received, InPlace, Gone };
(...)
dropAssetType.DataSource = Enum.GetValues(AssetType);
(...)

For some reason this get get compiled, since it says that AssetType is a
class, while a variable is expected,

Any suggestions woulde be appreciated!

Greets,

Gab
 
Hi Gabriël,

Change your code to

dropAssetType.DataSource = Enum.GetValues(typeof(AssetType));

Not exactly sure why just passing AssetType won't work, maybe someone can
clarify.

Happy coding!
Morten Wennevik [C# MVP]
 
Morten Wennevik said:
Change your code to

dropAssetType.DataSource = Enum.GetValues(typeof(AssetType));

Not exactly sure why just passing AssetType won't work, maybe someone can
clarify.

Because AssetType isn't an expression - it's the name of a type.
 
Um, you mean it isn't a type, just a name?
Well, I suppose, since GetValues requires a type, but somehow I would
expect it to work by passing enum name, and have GetValues handle the
typeof cast internally if needed.

Happy coding!
Morten Wennevik [C# MVP]
 
Morten Wennevik said:
Um, you mean it isn't a type, just a name?

Just "AssetType" isn't an expression. typeof(AssetType) is an
expression. AssetType is a type token in terms of the C# language
specification - but it certainly isn't an expression. If you think it's
an expression, what's its value?
Well, I suppose, since GetValues requires a type, but somehow I would
expect it to work by passing enum name, and have GetValues handle the
typeof cast internally if needed.

typeof isn't a cast - it's an operator.
 
But now that the Whidbey compiler lets you specify just a method name
where a delegate is expected (and the compiler figures out the correct
delegate type and adds the instantiation code), you could argue that
you should also be able to specify just a typename in places where a
Type is expected. Just a thought.



Mattias
 
Thanks for all the advices (and explaination, which makes also the why
clear, next tot the how).

Gab
 
Back
Top