Passing multiple enum values as one parameter

Reply

Join Date: Oct 2006
Posts: 113
Reputation: edek is an unknown quantity at this point 
Solved Threads: 0
edek's Avatar
edek edek is offline Offline
Junior Poster

Passing multiple enum values as one parameter

 
0
  #1
Nov 17th, 2007
:The problem:

enum:
  1. enum Choice
  2. {
  3. A,
  4. B,
  5. C
  6. }
I would like to pass it to function like that:
  1. void function(Choice.A | Choice.B)
  2. {
  3. //how do I determine what was passed here??
  4. //how to handle two or more values??
  5. }

Thanks in adv
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 30
Reputation: InfiNate is an unknown quantity at this point 
Solved Threads: 6
InfiNate InfiNate is offline Offline
Light Poster

Re: Passing multiple enum values as one parameter

 
0
  #2
Nov 17th, 2007
Look up how to use a switch statement.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 2
Reputation: Askold is an unknown quantity at this point 
Solved Threads: 0
Askold's Avatar
Askold Askold is offline Offline
Newbie Poster

Re: Passing multiple enum values as one parameter

 
0
  #3
Nov 18th, 2007
Originally Posted by InfiNate View Post
Look up how to use a switch statement.
First of all your enum must have values which equal to x^2 (1, 2, 4, 8, 16, 32, etc.).

enum Choice
{
A = 1,
B = 2,
C = 4
}

Having the such enumeration your method can look as the following does:

void function(Choice choice)
{
if ((choice & Choice.A) != 0)
{
// It's A.
}

if ((choice & Choice.B) != 0)
{
// It's B.
}
}
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 113
Reputation: edek is an unknown quantity at this point 
Solved Threads: 0
edek's Avatar
edek edek is offline Offline
Junior Poster

Re: Passing multiple enum values as one parameter

 
0
  #4
Nov 18th, 2007
Thanks for help!!!
Last edited by edek; Nov 18th, 2007 at 11:49 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 4
Reputation: 4M-world is an unknown quantity at this point 
Solved Threads: 0
4M-world's Avatar
4M-world 4M-world is offline Offline
Newbie Poster

Re: Passing multiple enum values as one parameter

 
0
  #5
Nov 19th, 2007
most easy way to do such thing use binary idea as Askold told you in his post: where
1: 0001
2: 0010
3: 0011
and so on...
then do your comparison to know each value of enumerator you want but be careful in such number like 4: 0011 when you and with it can retrieve bot A, and B in such case use number in such way so you can distinct between
A: 0000 0001 = 1
B: 0000 0010 = 2
C: 0000 0100 = 4
Who never risks he never drink Champagne
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 113
Reputation: edek is an unknown quantity at this point 
Solved Threads: 0
edek's Avatar
edek edek is offline Offline
Junior Poster

Re: Passing multiple enum values as one parameter

 
0
  #6
Nov 19th, 2007
Thanks!

I have already found another page where it is explained, but THANKS!
Reply With Quote Quick reply to this message  
Join Date: Dec 2003
Posts: 2,417
Reputation: alc6379 has a spectacular aura about alc6379 has a spectacular aura about alc6379 has a spectacular aura about 
Solved Threads: 125
Team Colleague
alc6379's Avatar
alc6379 alc6379 is offline Offline
Cookie... That's it

Re: Passing multiple enum values as one parameter

 
0
  #7
Dec 6th, 2007
Why not use a switch statement? Make a variable that can contain the value of your Enum, pass that as a parameter of the method, and then use a switch statement to evaluate the value of that variable in the method... Why would that not work?
Alex Cavnar, aka alc6379
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 10966 | Replies: 6
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC