简体   繁体   English

C# 中的“超出数组范围”错误

[英]“Out of bound from the array” error in C#

A beginner question here.这里是初学者的问题。 I'm trying to run this code.我正在尝试运行此代码。 I kinda want to alter the order of another array into this new array.我有点想将另一个数组的顺序更改为这个新数组。 However, I get error System.IndexOutOfRangeException: 'Index out of bound from array'但是,我收到错误 System.IndexOutOfRangeException: 'Index out of bound from array'

I don't really know what to do or what did I do wrong.我真的不知道该做什么或我做错了什么。

 public partial class Form3 : Form
    {
        public string[] arrayJugadores = new string[3];
        Form2 FormRegistro = new Form2();
        public Form3()
        {
            InitializeComponent();

            Random randomizador = new Random();
            int valor = randomizador.Next(1, 15);

            if (valor == 1)
            {
                arrayJugadores[0] = FormRegistro.listaJugadores[0];
                arrayJugadores[1] = FormRegistro.listaJugadores[1];
                arrayJugadores[2] = FormRegistro.listaJugadores[2];
                arrayJugadores[3] = FormRegistro.listaJugadores[3];
            }
            else if (valor == 2)
            {
                arrayJugadores[0] = FormRegistro.listaJugadores[3];
                arrayJugadores[1] = FormRegistro.listaJugadores[0];
                arrayJugadores[2] = FormRegistro.listaJugadores[1];
                arrayJugadores[3] = FormRegistro.listaJugadores[2];
            }

make your array bigger, it can currently only hold 3 elements:使您的数组更大,它目前只能容纳 3 个元素:

public string[] arrayJugadores = new string[4];

also, check the error message and what line it appears in, then you should be able to figure it out.另外,检查错误消息及其出现在哪一行,然后您应该能够弄清楚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM