Solved

Enum, Switch

Posted on 2006-06-14
5
461 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

830 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