Index was outside the bounds of the array (C#)

后端 未结 3 1582
[愿得一人]
[愿得一人] 2020-12-12 07:28

I get \"Index was outside the bounds of the array\" on this line, what\'s wrong?

Kort[x, y] = Sort[x] + Valor[y] + \" \";    

Below is the

相关标签:
3条回答
  • 2020-12-12 07:38

    Sort is an array 0..3, and Valor is 0..12. So you cannot use Sort[4] and Valor[13].

    0 讨论(0)
  • 2020-12-12 07:43

    Start your array accesses from 0, and not 1

    So, change to this:

    private void Form1_Load(object sender, EventArgs e)
                {
                    Valor[0] = "2";
                    Valor[1] = "3";
                    Valor[2] = "4";
                    Valor[3] = "5";
                    Valor[4] = "6";
                    Valor[5] = "7";
                    Valor[6] = "8";
                    Valor[7] = "9";
                    Valor[8] = "10";
                    Valor[9] = "Knekt";
                    Valor[10] = "Dam";
                    Valor[11] = "Kung";
                    Valor[12] = "Ess";
    
                    Sort[0] = "H";
                    Sort[1] = "R";
                    Sort[2] = "S";
                    Sort[3] = "K";
                }
    

    Also, start any of your loops at 0, instead of 1. And make the conditional be less than the length, not until equal. More like:

    for (int i=0; i < theArray.Length; i++)
    
    0 讨论(0)
  • 2020-12-12 07:54

    in C# arrays are zero-based...

    look at what Kevek answered you plus:

    this:

    for (this.x = 1; this.x <= 4; this.x++)
    {
      for (this.y = 1; this.y <= 13; this.y++)
      ...
    

    should be:

    for (this.x = 0; this.x < 4; this.x++)
    {
      for (this.y = 0; this.y < 13; this.y++)
      ...
    
    0 讨论(0)
提交回复
热议问题