Click here to Skip to main content
11,496,146 members (33,337 online)
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
See more: C#
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:

if (value == (6 || 8 || 10))
{
    size1 = value;
}
Posted 19-Jan-13 4:35am
Here's another way:

private enum MySet
{
    Six = 6
,
    Eight = 8
,
    Ten = 10
}
 
if ( System.Enum.IsDefined ( typeof(MySet) , i ) )
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 20-Jan-13 0:17am
   
Ha! Like this one, a 5.
—SA
Hi,

I found this:
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:
public static class MultipleOrConditionClass
{
    public static bool MultipleOrCondition(this object obj, params object[] objectsToCompare)
    {
        return objectsToCompare.Contains(obj);
    }
}
Now, use this condition:
  if (value.MultipleOrCondition(6, 8, 10))
            {
                size1 = value;
            }
Hope this helps.
  Permalink  
v2
Comments
Gilmore Sacco at 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
ProgramFOX at 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 ( new int[] { 6 , 8 , 10 } ).Contains ( value ) )

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?
  Permalink  
Comments
Gilmore Sacco at 19-Jan-13 10:26am
   
if it is not in set, i am setting as zero
Use switch statement

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

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

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);
    }
}
  if(value.In(6,8,10)){
    size1 = value;
  }
  Permalink  
v2
Comments
Tharaka MTR at 19-Jan-13 21:36pm
   
again someone down-voted without checking the complete answer ... :p
PIEBALDconsult at 20-Jan-13 8:09am
   
The original version wasn't nearly as good.
Tharaka MTR at 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)

  Print Answers RSS
0 Dnyaneshwar@Pune 692
1 CHill60 318
2 RyanDev 230
3 Sascha Lefèvre 205
4 OriginalGriff 171
0 Sergey Alexandrovich Kryukov 10,372
1 OriginalGriff 8,871
2 Sascha Lefèvre 3,899
3 Maciej Los 3,422
4 Richard Deeming 2,600


Advertise | Privacy | Mobile
Web03 | 2.8.150520.1 | Last Updated 19 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100