Solved

Enum, Switch

Posted on 2006-06-14
5
458 Views
Last Modified: 2010-04-16
Hi experts.

I would like to use an enum of roles to check against a int variable.

I have the following:

foreach(int roleId in currentUserRoles)
{
  switch(roleId)
  {
      case ((int)RoleType.Administrator):
             AdminOnly = true;
            break;
   }
}

what I need to happen is for an integer check to happen, but this does not seem to work. My enum is as follows:

public enum RoleType
{
    Operations = 0,
    Finance = 1,
    Administrator = 2
}

basically, whatever numbers are returned in roleId (1,3) then my code should check against the enum to see which role this links to then set a bool value respectively

Wayne
0
Comment
Question by:watherton
5 Comments
 
LVL 14

Accepted Solution

by:
existenz2 earned 32 total points
ID: 16900819
I don't really see a problem in this code. It seems quite efficient to me. What is the exact problem?
0
 
LVL 13

Assisted Solution

by:dungla
dungla earned 31 total points
ID: 16900823
Hi watherton,

Your code is correct and should work. Can you tell me what kind of error you met?
0
 
LVL 4

Assisted Solution

by:jvn222
jvn222 earned 31 total points
ID: 16901007
Your code is correct and should work!

to dungla: Bac dungla van kiem diem a, thang nay bac moi duoc co 1000 thoi a? :D
0
 
LVL 10

Assisted Solution

by:SystemExpert
SystemExpert earned 31 total points
ID: 16901042
Hi,

I think you need like this

foreach(int roleId in currentUserRoles)
{
      if (roleId==(int)RoleType.Administrator)
      {
            AdminOnly = true;
            break;
      }
                        
}

Thanks
0
 

Author Comment

by:watherton
ID: 16901074
my mistake,

thanks for the help chaps, it worked fine.

I will split the points
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video discusses moving either the default database or any database to a new volume.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now