Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Enum, Switch

Posted on 2006-06-14
5
Medium Priority
?
464 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 14

Accepted Solution

by:
existenz2 earned 128 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 124 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 124 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 124 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

722 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