Help with a simple enum error

S

Simon Harvey

Hi all,

I thought that if I declared an Enum as follows:

public enum RolesEnumeration{
GlobalAdmin = 0,
PrincipalTrialInvestigator = 1,
TrialManager = 2,
AssistantTrialManager = 3,
GroupCoordinator = 4,
GroupTrialManager = 5,
GroupDataManager = 6,
ClinicalResearchAssistant = 7,
StudyManagementCommittee = 8,
PrincipalSiteInvestigator = 9,
SiteCoInvestigator = 10,
SiteStudyNurses = 11,
Patient = 12,
IndustryUser = 13,
Others = 14,
LabResearcher = 15,
DataAndEthicsMonitoringCommittee = 16
}

Then I could make a statement like:
if(roleID <= RolesEnumeration.GolbalAdmin){
....

At the moment though, the compiler is telling me that:
Operator '<=' cannot be applied to operands of type 'int' and
'spirit.secure.RolesEnumeration'

Can anyone see what I've done wrong? I know its going to be something
totally obvious but I cant see it!

Thanks all

Simon
 
J

José Joye

Assuming your RoleID is an int, you should do the following:
if(roleID <=(int) RolesEnumeration.GolbalAdmin){

José
 
D

Dmitriy Lapshin [C# / .NET MVP]

Hi Simon,
Can anyone see what I've done wrong? I know its going to be something
totally obvious but I cant see it!

You should cast the enumeration item to int:

if(roleID <= (int)RolesEnumeration.GolbalAdmin)

given roleID is an "int" variable.
 
J

Jon Skeet [C# MVP]

Simon Harvey said:
I thought that if I declared an Enum as follows:

public enum RolesEnumeration{
GlobalAdmin = 0,
PrincipalTrialInvestigator = 1,
TrialManager = 2,
AssistantTrialManager = 3,
GroupCoordinator = 4,
GroupTrialManager = 5,
GroupDataManager = 6,
ClinicalResearchAssistant = 7,
StudyManagementCommittee = 8,
PrincipalSiteInvestigator = 9,
SiteCoInvestigator = 10,
SiteStudyNurses = 11,
Patient = 12,
IndustryUser = 13,
Others = 14,
LabResearcher = 15,
DataAndEthicsMonitoringCommittee = 16
}

Then I could make a statement like:
if(roleID <= RolesEnumeration.GolbalAdmin){
...

At the moment though, the compiler is telling me that:
Operator '<=' cannot be applied to operands of type 'int' and
'spirit.secure.RolesEnumeration'

Can anyone see what I've done wrong? I know its going to be something
totally obvious but I cant see it!

Well, you haven't shown how you've declared roleID, but I'm assuming
it's as an int rather than a spirit.secure.RolesEnumeration. Cast
either side to the same type as the other and it should be fine, ie:

if ((RolesEnumeration)roleID <= RolesEnumeration.GlobalAdmin)

or

if (roleID <= (int)RolesEnumeration.GlobalAdmin)
 
S

Simon Harvey

Thanks everyone!

I knew it would be something simple like that. I thought the compiler would
know it was an int I was dealing with.

I'll remember that for the future.

Thanks again everyone

Kindest Regards

Simon
 
J

Jon Skeet [C# MVP]

Simon Harvey said:
Thanks everyone!

I knew it would be something simple like that. I thought the compiler would
know it was an int I was dealing with.

Fortunately not. If it did, you'd just have got rid of a lot of type-
safety, and you might as well just use integer constants :)
 
Top