2

I'm trying to make a class called LoopingInt. It stores two integers, one being the maximum value of the integer, and the other being a stored integer. When the integer falls below 0 or above the maximum value, it "loops" back around. So if you add 3 to a LoopingInt with a value of 4, and the max value is 6, the internally stored integer in the class will be 7, but asking for the integer externally will return 0.

What I want to do is make it so I can work with LoopingInts as if they were integers. I can already assign LoopingInts to int objects (ie int x = myLoopingInt), but I can't assign an int to a LoopingInt because I can't figure out how to pass back a LoopingInt object with the right maximum value. I need the maximum value from the left-hand value, but I don't know how to get it.

3
  • 1
    I would name it WraparoundInt or something similar, as the notion of integer overflow causing the value to wraparound is familiar to many programmers. Commented Nov 20, 2010 at 23:01
  • FYI what you are constructing is a modular arithmetic system on a congruence class of integers; thereby you form a commutative ring. See en.wikipedia.org/wiki/Modular_arithmetic for some mathematical background that might be helpful to you. Commented Nov 20, 2010 at 23:21
  • Sounds like a good idea. I'll do that. Commented Nov 20, 2010 at 23:22

3 Answers 3

2

If you're asking how to fix:

LoopingInt myLoopingInt = new LoopingInt(4, 10);
myLoopingInt = x;

so that myLoopingInt's Value member is modified but the MaxValue member stays the same then I don't think its possible. You can set a property instead:

myLoopingInt.Value = x;
Sign up to request clarification or add additional context in comments.

1 Comment

For now I think I'll just have to accept this fact, as irritating as it may be.
0

You can write an implicit conversion operator:

public static implicit operator LoopingInt(int i)
{
  return new LoopingInt(i);
}

1 Comment

Well, my idea already is to use an implicit operator, however, what I need to do is something like this: myLoopingInt = 5 With the maximum value of myLoopingInt remaining the same. To do that I need to get that value from the left-hand value, and I don't know how.
0

Well, you have to decide the semantics that you want:

class LoopingInt32 {
    // details elided

    public LoopingInt32(int maximumValue, int value) { // details elided }

    public static implicit operator LoopingInt32(int x) {
        int maximumValue = some function of x; <-- you implement this
        int value = some other function of x;  <-- you implement this
        return new LoopingInt32(maximumValue, value);
    }
}

We can't decide this for you.

Edit: What you're asking for is completely impossible The right-hand-side of an assignment never knows about the left-hand-side. There might not even be a left-hand-side (consider SomeFunctionThatEatsLoopingInt32(5))!

2 Comments

Sorry, should have been clearer on my question. maximumValue is independent of the value of x. It is stored in the left-hand value but I don't know how to get it. value I already know how to get - it is simply x.
@Timballisto: Oh. Then what you're asking is completely impossible. The right-hand-side of an assignment never knows about the left-hand-side.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.