2
\$\begingroup\$

Sorry if this question has been asked before, but I am having trouble searching for the answer I am looking for. This question is in regards to efficiency and overall C# standards.

I have an integer that I manipulate by 1, either by -- or ++ depending on a user's input. I then use this integer as an index to grab an object out of an array. I would like to know what the best way to make sure this variable will always stay within the confines of the array, such that if the user reached the end of the array, it would loop back to the beginning and continue from there. This is an example of the logic I am using for that:

switch(directionMoved)
{
    case MoveDirection.Left:
        currentPageIndex--;
        break;

    case MoveDirection.Right:
        currentPageIndex++;
        break;
}

if (currentPageIndex < 0)
{
    var distanceFromZero = currentPageIndex;
    currentPageIndex = objectArray.Length + distanceFromZero;
}

if (currentPageIndex >= objectArray.Length)
{
    var distanceFromEnd = currentPageIndex - objectArray.Length;
    currentPageIndex = distanceFromEnd;
}

...

(use 'currentPageIndex' to grab from array)

Is there a cleaner way to do this same logic? I hope my questions was clear... =]

Thanks!

\$\endgroup\$
1
  • \$\begingroup\$ Looks like this question migrated over on its own.. or a moderator did it. In any case, thanks! \$\endgroup\$ Apr 20, 2016 at 18:15

3 Answers 3

2
\$\begingroup\$

After you alter the currentPageIndex in order to keep it inside of the array you have to process it in one of the following ways

In case you want to wrap around the array:

// this method is called wrap
currentPageIndex = (currentPageIndex + objectArray.Length) % objectArray.Length;

In case you want to stop at the end of array:

// this method is called clamp
if (currentPageIndex < 0)
    currentPageIndex = 0;
if (currentPageIndex >= objectArray.Length)
    currentPageIndex = objectArray.Length - 1;
\$\endgroup\$
2
\$\begingroup\$

You can use % remainder operator to be sure the value will be within bounds:

if (directionMoved == MoveDirection.Left) currentPageIndex--;
if (directionMoved == MoveDirection.Right) currentPageIndex++;

currentPageIndex += objectArray.Length;
currentPageIndex %= objectArray.Length;

This wiil allow you iterate in a circular way between 0 and objectArray.Length - 1.

\$\endgroup\$
2
  • \$\begingroup\$ Fascinating. I knew this type of logic could be more concise. Thank you! \$\endgroup\$
    – hellaandrew
    Apr 15, 2016 at 17:28
  • \$\begingroup\$ Accepting as answer unless someone can do better =] \$\endgroup\$
    – hellaandrew
    Apr 15, 2016 at 17:29
0
\$\begingroup\$

Triggered by hellaandrew's comment I had to come with another (better?) solution:

Given:

public enum MoveDirection 
{ 
    Left = -1, 
    Right = 1 
};

You could do:

currentPageIndex += (int)direction;
return currentPageIndex < 0 
    ? objectArray.Length - 1 
    : currentPageIndex % objectArray.Length;
\$\endgroup\$
1
  • \$\begingroup\$ I ended up using @JanKytka's response for what I was working on since other people will be reading my code. You get point from me for fancyness =]. However, it looks like this question migrated to Code Review and I don't have enough points here to make any sort of adjustments.. \$\endgroup\$ Apr 20, 2016 at 18:17

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.