0

I want to have a number (let's say i) whose range is between 0 to 26 so that when the number is 26 and it is incremented by 1 (say i++) the value returns to 0 (i.e. the value is circular).

Is there such a thing in c#? What is it called? If not then how would I implement this in code (overloaded operators are accepted).

3
  • 1
    I'm not really sure I understand why this is complex in any way. Is i used in a for-loop or something? Give us some sample code of how you'd use it. Commented Apr 3, 2013 at 14:44
  • I don't know of any sort of 'boundaries' or rules, you can "set" for an int in the way you want. I'd suggest creating an if statement, or two, to control it. Commented Apr 3, 2013 at 14:45
  • 2
    Please show a concrete example on how you want to use this. One simple way to always receive values between 0 and 26 would be to use the modulo operator: var v = i % 27; Commented Apr 3, 2013 at 14:45

6 Answers 6

6

Make a property that limits the value:

private int _value;

public int Value {
  get { return _value; }
  set { _value = value % 27; }
}

Now when you increase the property the setter will limit the value.

Example:

Value = 25;
Value++; // Value is now 26
Value++; // Value is now 0
Value++; // Value is now 1
Sign up to request clarification or add additional context in comments.

2 Comments

Good idea! I like the thought of creating a property with set rules, to control it.
This would be the simplest solution :)
4

You can try this:

 int result = number % 27;

3 Comments

+1 this is the idea used in hash tables to make sure the index is within 0..N - 1.
Corrected to 27, as per Daniel comment.
For clarity you could do const int N = 26; int result = number % (N + 1);.
2

Use modulus operator (%)

var x = 0;
x = (x+1) % 27;

if you want it to go 0,1,2,3, ..... 24,25,26, 0, 1, 2, 3, ...

use modulus 27

1 Comment

Use x+1 rather than ++x on the right hand side. There is no point in storing the value back into the variable twice.
0

I don't know of any sort of 'boundaries' or rules, you can "set" for an int in the way you want. I'd suggest creating an if statement, or two, to control it. `

if( i <= 26 & i >= 0)
{ ..do something..} 
else i = 0;

Comments

0

Something like this should accomplish what you ask:

class CircularInt
{
    public int value;

    public static CircularInt operator ++(CircularInt c)
    {
    if (c.value >= 26)
        c.value = 0;
    else
        c.value++;
    return c;
    }
}

Then use it:

CircularInt cInt = new CircularInt();
cInt++;
Console.WriteLine(cInt.value);

1 Comment

I like this method, but the property method is simpler; as it doesn't need to overload any operators.
0

Another option is to define your own immutable type.

public struct Value27
{
    private readonly int val;
    private readonly bool isDef;
    private Value27(int value)
    {
       while (value < 0) value += 27;
       val = value % 27;
       isDef = true;
    }
    public static Value27 Make(int value)
    { return new Value27(value); }

    public bool HasValue { get { return isDef; } }
    public int Value { get { return val; } }

    public static Value27 operator +(Value27 curValue)
    { return Make(curValue.Value + 1); }
    public static Value27 operator -(Value27 curValue)
    { return Make(curValue.Value + 26); }

    public static implicit operator Value27(int bValue)
    { return Make(bValue); }
    public static implicit operator int (Value27 value)
    { return value.Value; }
}

2 Comments

So then this way you just declare Value27 i =26; i++ and it becomes 0?
Yes, exactly..., or declare Value27 I = 26; i--; and it becomes 0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.