0

I'm still new in c#. The code been working for months without error until recently. It is constantly throw out the error: Unhandled Exception System.IndexOutofRangeException: Index was outside the bounds of the array.

I'm not sure where the error happening in the code.

private Random rnd = new Random();
string[] slots = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13" };
private int slot1, slot2, slot3 = 0;

        if (message.Equals("!game"))
        {
            if (user == luckychance)
            {
                slot1 = 0;
                slot2 = 0;
                slot3 = rnd.Next(0, 6);
                luckychance = "";
            }
            else
            {
                slot1 = rnd.Next(0, 14);
                slot2 = rnd.Next(0, 14);
                slot3 = rnd.Next(0, 14);
            }
            if (slot1 == slot2 && slot1 == slot3)
            {
                sendMessage(slots[slot1] + " | " + slots[slot2] + " | " + slots[slot3] + " win", 2);
            }
            else
            {
                sendMessage(slots[slot1] + " | " + slots[slot2] + " | " + slots[slot3] + " lost", 2);
            }
        }
6
  • which line throws the error? Commented Feb 3, 2015 at 22:47
  • Assuming it's when you use the result of the rnd.Next call (hint if it isn't), read the documentation carefully. Commented Feb 3, 2015 at 22:49
  • there is more to this code where the message is received and that part throws the array error. normally when that happens it is related to the function the message calls. Commented Feb 3, 2015 at 22:49
  • 3
    @PrestonGuillot: The upper bound of Next is exclusive but still too high by one (zero-based arrays). Commented Feb 3, 2015 at 22:52
  • you may have lost track that your array is actually 0 indexed even though you started your array at "1". Commented Feb 3, 2015 at 22:53

2 Answers 2

2
slot1 = rnd.Next(0, 14);

Returns a value from 0..13

You only have 13 "slots" but are picking from 14 possible slot positions.

When a slot is 13 (maximum you might get), you will try to access the 13th index of slots, which does not exist.

Sign up to request clarification or add additional context in comments.

3 Comments

I was told Next (0, 14) where 14 is the max bound and it is not included. Am I wrong? Thank you. Maybe I'm confused with the 0.
14 is not included, but 13 is also too large. Your array has indices from 0 to 12 (remember, arrays in C# are zero-based. The first element is slots[0].
Thank you so much it makes more sense now.
1

You have 13 elements in your slots array which is zero indexed so the largest possible index is 12. You assign a random value which is less than 14 so when the random value is greater than 12 it is outside the bounds of your array.

1 Comment

Yeah, C# is 0-indexed.

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.