Enums and types, does the enum really take the type?

  • Thread starter Thread starter SpotNet
  • Start date Start date
S

SpotNet

Hello NewsGroup,

All the best for the New Year to you all. Have a question regarding enums
and the integer types that can be scoped as. Briefly, if I scope an enum as
uint, I cannot use the enum as a uint may be used generally, but must use it
as the enum.

For example:

public enum SomeUInt32Constants: uint
{
ThisConstant = 0x01121,
ThatConstant= 0x01123,
}

A function that has a uint parameter, or a value from SomeUInt32Constants
enum.

public void OperationDependingOnParamter(uint uiAconstantValue)
{

}

I cannot make the following call,
OperationDependingOnParamter(SomeUInt32Constants.ThisConstant). I can, but I
receive the appropriate casting error. I know this question is academic in
nature, though I ask on a pratical reason, is
SomeUInt32Constants.ThisConstant a uint (UInt32)? Please I'm aware of the
various approaches in passing a predefined constant (as well as any other
value not defined), my question is purely on the basis asked.

Thank you all very much NewsGroup, your responses are very much appreciated.

Regards and Happy New Year to you all,
SpotNet.
 
I cannot make the following call,
OperationDependingOnParamter(SomeUInt32Constants.ThisConstant). I can,
but I receive the appropriate casting error. I know this question is
academic in nature, though I ask on a pratical reason, is
SomeUInt32Constants.ThisConstant a uint (UInt32)? Please I'm aware of
the various approaches in passing a predefined constant (as well as
any other value not defined), my question is purely on the basis
asked.

This is because .NET considers enum's to be "a different type" from the
underlying int/uint/long/etc value... its primarily for type safety, to
prevent you from doing something you didn't mean to do. Because a uint can
be anything, but the enum can only be certain values, so from a number-set
standpoint, they are different.

If you want to pass in a uint to the function, you can cast it as a uint
and it will work fine. As you say, a possibly better approach is to define
these values a const uints in a class.

-mdb
 
Thank you very much Michael for the explaination.

Regards,
SpotNet.

: :
: > I cannot make the following call,
: > OperationDependingOnParamter(SomeUInt32Constants.ThisConstant). I can,
: > but I receive the appropriate casting error. I know this question is
: > academic in nature, though I ask on a pratical reason, is
: > SomeUInt32Constants.ThisConstant a uint (UInt32)? Please I'm aware of
: > the various approaches in passing a predefined constant (as well as
: > any other value not defined), my question is purely on the basis
: > asked.
: >
:
: This is because .NET considers enum's to be "a different type" from the
: underlying int/uint/long/etc value... its primarily for type safety, to
: prevent you from doing something you didn't mean to do. Because a uint
can
: be anything, but the enum can only be certain values, so from a number-set
: standpoint, they are different.
:
: If you want to pass in a uint to the function, you can cast it as a uint
: and it will work fine. As you say, a possibly better approach is to
define
: these values a const uints in a class.
:
: -mdb
 
Back
Top