| | |
Passing multiple enum values as one parameter
![]() |
:The problem:
enum: I would like to pass it to function like that:
Thanks in adv
enum:
C# Syntax (Toggle Plain Text)
enum Choice { A, B, C }
C# Syntax (Toggle Plain Text)
void function(Choice.A | Choice.B) { //how do I determine what was passed here?? //how to handle two or more values?? }
Thanks in adv
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.
}
}
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.
}
}
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
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
![]() |
Similar Threads
- Creating Crystal report using MS Access query (VB.NET)
- Passing Variable Values as a parameter Name in MySQL (MySQL)
- problem in passing multiple checkbox values (ASP)
- passing empty string to SqlCommand.Parameter.Add (VB.NET)
- Passing Variables/Parameters - By Ref/Value? (Computer Science)
- need help with code (C++)
Other Threads in the C# Forum
- Previous Thread: retrieve time from database
- Next Thread: Writing Xml From datasource with DTD
Views: 10966 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C#
.net 2d access algorithm application array asp.net audio bitmap box broadcast button c# calendar call check checkbox class code color control conversion csharp custom data database datagrid datagridview dataset datatable datetimepicker degrees display dll drawing event excel file form format forms function game gcd gdi+ graphics image index input internet list listbox login mandelbrot math multithreading mysql office operator pda picturebox pixel print procedure programming query recursion remote remoting resource richtextbox round save saving search server sleep socket sql start statistics stream string text textbox threading time timer update validation vc++ video visual visualstudio webbrowser windows winforms wpf write xml






