Click here to Skip to main content
15,886,101 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Dear experts,

This will only take you 1 min max. I need to make a multiple conditional OR statement in a if statement. I know I can or them seperately, but is there a shortcut. This is my failed attempt:

C#
if (value == (6 || 8 || 10))
{
    size1 = value;
}
Posted

Here's another way:

private enum MySet
{
    Six = 6
,
    Eight = 8
,
    Ten = 10
}

if ( System.Enum.IsDefined ( typeof(MySet) , i ) )
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 20-Jan-13 0:17am    
Ha! Like this one, a 5.
—SA
Hi,

I found this:
C#
if ((value | 6 | 8 | 10) == (6 | 8 | 10) && value != 0)
{
    size1 = value;
}

In this case, there's no many difference in length of your condition. If you've variables with long names, or if many options are possible, this is a shorter condition.

Alternatively, you can write an extension method[^] for objects:
C#
public static class MultipleOrConditionClass
{
    public static bool MultipleOrCondition(this object obj, params object[] objectsToCompare)
    {
        return objectsToCompare.Contains(obj);
    }
}

Now, use this condition:
C#
if (value.MultipleOrCondition(6, 8, 10))
          {
              size1 = value;
          }

Hope this helps.
 
Share this answer
 
v2
Comments
Gilmore Sacco 20-Jan-13 8:06am    
Thank you very muxh, programFOX, it worked like a charm. I do have another issue, if you have the time to help me. I would really appriceate it: http://www.codeproject.com/Questions/531057/CheckplusForplusnullplusvalueplusinplusCSVplusstri
Thomas Daniels 20-Jan-13 8:50am    
Thank you very muxh
You're welcome!
No.

But you could use a HashSet

if ( new System.Collections.Generic.HashSet<int> ( new int[] { 6 , 8 , 10 } ).Contains ( value ) )</int>

Though if you did that you should make the HashSet a static readonly field.


I also wonder what you want to do when the value isn't in the set?
 
Share this answer
 
Comments
Gilmore Sacco 19-Jan-13 10:26am    
if it is not in set, i am setting as zero
Use switch statement

C#
switch (value)
{
   case 6:
   case 8:
   case 10:
     size1 = value;
  break;
}


or if you really want to do this create extension method.

C#
static class Extensions
{
    public static bool In<T>(this T value, params T[] values)
    {
        if (values == null)
            throw new ArgumentNullException("values");

        return values.Contains(value);
    }
}

C#
if(value.In(6,8,10)){
  size1 = value;
}
 
Share this answer
 
v2
Comments
Tharaka MTR 19-Jan-13 21:36pm    
again someone down-voted without checking the complete answer ... :p
PIEBALDconsult 20-Jan-13 8:09am    
The original version wasn't nearly as good.
Tharaka MTR 20-Jan-13 8:13am    
Ok. I am agreed. first I posted only SWITCH statement. But Within a minute of time I posted the extension method. If you downvoted just seen the SWITCH statement, I'm agreed with you. My bad. sorry...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900