-1

I am trying to print out a value from my array but the System.IndexOutOfRangeException keeps coming up! I'm not too sure whats wrong!

Code For My button:

private void Button_Click(object sender, RoutedEventArgs e)
    {
        Customers.CustomersArray[0, 6] = "12";
        MessageBox.Show(Customers.CustomersArray[0, 6]);

    }

Code From the class:

class Customers
{
    public static string[,] CustomersArray = new string[10, 6];

}
  • 2
    Does this answer your question? [What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?](https://stackoverflow.com/questions/20940979/what-is-an-indexoutofrangeexception-argumentoutofrangeexception-and-how-do-i-f) – Yong Shun Apr 24 '21 at 00:21

1 Answers1

0

This means that you are creating a string with length of 10 and 6

new string[10, 6];

And assigning value always starts from 0 index So you should change your code to this

Customers.CustomersArray[0, 5] = "12";
MessageBox.Show(Customers.CustomersArray[0, 5]);

Here 5 represents the last index of memory because it starts from 0 and total possible values for this string will be [0-9,0-5]

Hammad Shabbir
  • 722
  • 1
  • 6
  • 13