System.IndexOutOfRangeException?

时间:2014-10-29 20:09:56

标签: c# arrays

我制作了一个小型数据库程序,并且在尝试初始化过去[currentIndexPlayer,2]之前我遇到了错误System.IndexOutOfRangeException,之后一切正常。我怎么可能在阵列中用完了空间?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string[,] playerArray = new string[19, 7]; // Initial player array is declared, this is where the players are stored//
        int currentIndexPlayer = 0;

        private void addNewPlayerBtn_Click(object sender, EventArgs e)
        { 
            if (addNewPlayerBtn.Text == "Add New Player")
            {
                playerFirstNameBox.ReadOnly = false; //Unlock all fields for input//
                playerIgNameBox.ReadOnly = false;
                playerSecondNameBox.ReadOnly = false;
                contactStreetBox.ReadOnly = false;
                contactTownBox.ReadOnly = false;
                contactPostcodeBox.ReadOnly = false;
                contactEmailBox.ReadOnly = false;
                contactTelephoneBox.ReadOnly = false;

                addNewPlayerBtn.Text = "Confirm"; 
            }
            else if (addNewPlayerBtn.Text == "Confirm")
            {
                if ((playerFirstNameBox.Text != "") && (playerIgNameBox.Text != "") && (playerSecondNameBox.Text != "")) // Validation check to see if all main fields were entered
                {
                    playerArray[currentIndexPlayer, 0] = playerFirstNameBox.Text;
                    playerArray[currentIndexPlayer, 1] = playerIgNameBox.Text;
                    playerArray[currentIndexPlayer, 2] = playerSecondNameBox.Text;
                    playerArray[currentIndexPlayer, 3] = contactStreetBox.Text;
                    playerArray[currentIndexPlayer, 4] = contactTownBox.Text;
                    playerArray[currentIndexPlayer, 5] = contactPostcodeBox.Text;
                    playerArray[currentIndexPlayer, 6] = contactEmailBox.Text;
                    playerArray[currentIndexPlayer, 7] = contactTelephoneBox.Text;
                    currentIndexPlayer++;

                }
                else
                {
                    MessageBox.Show("Missing fields","Problem With Input", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); //Validation failed, display error message//
                    addNewPlayerBtn.Text = "Confirm";
                }

                addNewPlayerBtn.Text = "Add New Player";

                playerFirstNameBox.ReadOnly = true; //lock all fields for input again//
                playerIgNameBox.ReadOnly = true;
                playerSecondNameBox.ReadOnly = true;
                contactStreetBox.ReadOnly = true;
                contactTownBox.ReadOnly = true;
                contactPostcodeBox.ReadOnly = true;
                contactEmailBox.ReadOnly = true;
                contactTelephoneBox.ReadOnly = true;

            }
        }

        private void searchToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form2 searchForm = new Form2();
            searchForm.Show();
        }

    }
}

3 个答案:

答案 0 :(得分:3)

数组索引基于零。在此行中,您尝试设置第七个元素,但最后一个是6

playerArray[currentIndexPlayer, 7] = contactTelephoneBox.Text;

此外,您需要确保currentIndexPlayer等于或小于第一维尺寸的18

答案 1 :(得分:1)

可能是你的阵列:

 string[,] playerArray = new string[19, 7];

在第二个维度中创建七个空格,稍后尝试分配到第八个空格:

playerArray[currentIndexPlayer, 7] = contactTelephoneBox.Text;

尝试将初始化中的七个更改为八并查看是否有效。

答案 2 :(得分:0)

您需要检查currentIndexPlayer< 19之前:playerArray [currentIndexPlayer,0]。

相关问题