-1

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'

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];
            }
halfer
  • 19,824
  • 17
  • 99
  • 186
Juan
  • 3
  • 1
  • 2
    Possible duplicate of [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) – Ňɏssa Pøngjǣrdenlarp Nov 09 '19 at 22:49

1 Answers1

3

make your array bigger, it can currently only hold 3 elements:

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.

Charles
  • 2,721
  • 1
  • 9
  • 15