c# max enum value

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

Is there a way (short of looping through every value) to determine the
maximum value of an enum?

So if I have this enum:

enum GradeLevel: byte
{
Unknown = 0,
GradePreK = 1,
GradeK = 2,
Grade1 = 3,
Grade2 = 4
}

The max value is 4 (not 255).

Thank You
-Keith
 
Keith Harris said:
Hi,

Is there a way (short of looping through every value) to determine the
maximum value of an enum?

So if I have this enum:

enum GradeLevel: byte
{
Unknown = 0,
GradePreK = 1,
GradeK = 2,
Grade1 = 3,
Grade2 = 4
}

The max value is 4 (not 255).

You should be able to use Enum.GetValues() and walk that array.
 
Why do you want to know? The numeric values of enum members are/should
be totally arbitrary. Most compilers default to making them sequential
integer values if specific values are not assigned, but their numeric
value is meaningless. I can see why you might reasonably want to know
the _number_ of members of an enum, but not the max member value.

Hi,

Is there a way (short of looping through every value) to determine the
maximum value of an enum?

So if I have this enum:

enum GradeLevel: byte
{
Unknown = 0,
GradePreK = 1,
GradeK = 2,
Grade1 = 3,
Grade2 = 4
}

The max value is 4 (not 255).

Thank You
-Keith


Please respond to the Newsgroup, so that others may benefit from the exchange.
Peter R. Fletcher
 
Quite often we have a StartValue and EndValue values as first and last value
within the enum.

enum GradeLevel: byte
{
Unknown = 0,
GradePreK = 1,
GradeK = 2,
Grade1 = 3,
Grade2 = 4,
EndValue = 5 //keep this at the end, add new values before this and assign
correct value.
}

In the code You can regard anything equal or above EndValue as invalid.

--Saurabh
 
Hello Saurabh,
In the code You can regard anything equal or above EndValue as
invalid.

Perhaps the better choice to see if an enum is invalid is Enum.IsDefined.
 
Sorry for misinformation, I wanted to stress that the EndValue which would
be a valid enum should also be treated as invalid in the code.

Thanks for pointing it out Matt !!

--Saurabh
 
You missed Matt's (and my) point. You are creating a system which
relies on the programmer being disciplined (never a good plan!) to do
something (parameter checking) which could be done much more safely by
using a predefined property of the enum Class. It could, indeed, be
argued that the compiler shouldn't let you do > or < comparisons with
enums, since the concept of < or > may not really be meaningful in
this context (in a colours enum, is brown greater than green or less
than green?). In addition, the error checking you are doing should be
totally unnecessary if you are using your enum properly, since the
compiler will not allow an enum member to have an invalid value unless
you cast an improperly valued variable of its base type into it.

Sorry for misinformation, I wanted to stress that the EndValue which would
be a valid enum should also be treated as invalid in the code.

Thanks for pointing it out Matt !!

--Saurabh


Please respond to the Newsgroup, so that others may benefit from the exchange.
Peter R. Fletcher
 
Hi

By reflection we can solve this proble. This is a sample enumeration.

enum EnuTest:byte
{
one,
two,
three
}

Then we get the type of this enumeration and try to retrieve all the fields
which are actually members of the enumeration. It is an array so we get the
last item which is typiucally the maximum value. In order to get the actual
numeric value not the text (In this example the maximum value is 2 not three
) we cast it after knowing the type of each member.

private void button1_Click(object sender, System.EventArgs e)
{

EnuTest et = new EnuTest() ;
Type enuType = typeof(EnuTest) ;

FieldInfo[] fi = enuType.GetFields() ;

Type filedType = fi[0].GetValue(et).GetType() ;
object o = Convert.ChangeType(fi[fi.Length-1].GetValue(et),filedType) ;
MessageBox.Show(o.ToString()) ;

}
 
Right, I understand it.
What do you suggest should be the answer to the OP?
I agree with Matt on the range thing and you had said that you could not see
"why anybody would want to know the max value" for which I think I did miss
your point.

I could see one use case in the OP. here's an example, you have defined
enums for a few different types of errors, typically categorising them into
UI error, App error, third party error so on and so forth.... now you define
the enum, and you actually want these error codes to be sequential so for
the second enum, your start value would be 1 more than the last enum. This I
think is where you would want to know the max value in an enum.

I think you will now say that derive from an exception and have different
exception classes for these different types of codes, but that still doesn't
answer the OP.

I guess, If I was Keith, I would investigate the feasibility of Matt's
suggestion.

Regards,

--Saurabh
 
Hello Saurabh,
I think you will now say that derive from an exception and have
different exception classes for these different types of codes, but
that still doesn't answer the OP.

This would be incorrect as well. Exceptions are for exceptional circumstances,
not to be used for controlling program flow.
 
Back
Top