2

I have 6 Ranges:

1000000-5000000
50000001-10000000
10000001-25000000
25000001-50000000
50000001-75000000
75000001-100000000

Now how do I say that:

var range1 = 1000000-50000000;
var range2 = 50000001-10000000;
int limit = 10000000;

if(limit and range1)
{
     result = 10;
}
else if(limit and range2)
{
     result = 20;
}

So what i am saying is if there is a combination of limit and range 1 then the result is 10? How would I do this?

Shaharyar
  • 12,254
  • 4
  • 46
  • 66
Mike Barnes
  • 4,217
  • 18
  • 40
  • 64
  • 14
    There is no built-in data type in C# that represents a range. You'll have to write your own. – John Saunders Jul 02 '13 at 09:23
  • 6
    Why not use the signs > and < in your if statement? – Joe Slater Jul 02 '13 at 09:23
  • Are you trying to say tha the 'limit' should be within the range? – reggaemahn Jul 02 '13 at 09:24
  • 1
    Beware of a joke: `if( Enumerable.Range(1,1000).Contains(5) ) ...` – quetzalcoatl Jul 02 '13 at 09:24
  • 1
    I couldn't understand your question can you elaborate more – Ibrahim Jul 02 '13 at 09:25
  • 1
    `var range1 = 1000000-50000000;` - var keyword just creates a variable based on the right side of production and it is an integer, not a range. – Kamil T Jul 02 '13 at 09:25
  • 3
    Related [Interval data type for C# .NET?](http://stackoverflow.com/q/4157467/73226) – Martin Smith Jul 02 '13 at 09:25
  • Well the limit is 10000 and the range is 1000000 then the result is 10 @Ibrahim – Mike Barnes Jul 02 '13 at 09:30
  • After rereading your question several times, I fail to understand what you say. Let's start with basic terms and clarifications: Limit of what? Range of what? What do you compare them with? Is the Range absolute, or relative to limit? Is the limit a lower bound of the range? And, more importantly, why does the first range has `1000000-5000000` (milion-5milions) and the second `50000001-10000000` (50milions-10milions). – quetzalcoatl Jul 02 '13 at 09:50

3 Answers3

13

You could try making some little anonymous (or not-so-anonymous, for re-use) functions:

Func<int, bool> range1 = i => (1000000 >= i) && (i <= 50000000);
Func<int, bool> range2 = i => (50000001 >= i) && (i <= 10000000);
Func<int, bool> limit =  i => i <= 10000000;

var test = 2000000;

if(limit(test) && range1(test))
{
     result = 10;
}
else if(limit(test) && range2(test))
{
     result = 20;
}
Ann L.
  • 13,760
  • 5
  • 35
  • 66
3

If your ranges are continuous like in the examples you gave, then please note that you do not need any 'interval'.

Continuous ranges like 1-9, 10-19, 20-29 actually define a "threshold points": 9|10, 19|20 and so on. Instead of checking a 1-9, 10-19, 20-29 ranges, you may simply:

if ( x <= 9 )
   ...
else if ( x <= 19 )
   ...
else if ( x <= 29 )
   ...

Note that the else part guarantees you the lower bound in each case.

EDIT:

You've updated your code with result = 10 and etc. If you really need only such simple operation, then you can define a list:

var levelsAndValues = List<Tuple<int, int>>();
levelsAndValues.Add(Tuple.Create(5000000, 10));
levelsAndValues.Add(Tuple.Create(10000000, 20));
...

and run a simple loop over it:

int limit = 1000000;
int result = 0;
foreach(var level in levelsAndValues)
    if(limit > level.Item1)
        result = level.Item2;
    else
        break;

or linq-lookup:

var result = levelsAndValues.Where(level => limit > level.Item1)
                            .Select(level => level.Item2)
                            .LastOrDefault();

Now, if your ranges are noncontiguous - you just have to introduce third value to the tuple: {low,high,value} instead of just {high, value} like I wrote above, and then update the filtering accordingly. This might be a good time to also change the Tuple to a custom type.

Or, to use the Interval datatype posted here, just like Marting hinted in the comments.

Community
  • 1
  • 1
quetzalcoatl
  • 32,194
  • 8
  • 68
  • 107
2

You can declare class about this:

public class Range
{
    public int Min { get; set; }
    public int Max { get; set; }
    public int Limit { get; set; }
    public int Result{get;set;}

    public Range(int min, int max, int limit, int result)
    {
        this.Min = min;
        this.Max = max;
        this.Limit = limit;
        this.Result = result;
    }

    public bool InRange(int value)
    {
        if (value >= this.Min && value <= this.Max && value <= limit)
            return true;
        return false;
    }
}

and use this class like:

List<Range> rangeList = new List<Range>();
        rangeList.Add(new Range(1000000, 5000000, 10000000, 10));
        rangeList.Add(new Range(5000001, 10000000, 10000000, 20));
        int? result = rangeList.Where(t => t.InRange(value)).Select(t => t.Result).FirstOrDefault();

if variable result is not null, it's your final result.

Bridge
  • 29,818
  • 9
  • 60
  • 82
Max
  • 66
  • 1