Solved

Enum, Switch

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
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…

777 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