Problem with arrays, index exceeded c # [closed]


I have a class that gets data of API , I'll explain all the code because I really do not know where the problem is.

 public static void Obtener_Enemigos()
            RiotSharp.RiotApi Api = RiotApi.GetDevelopmentInstance(_Key);
            RiotSharp.StaticRiotApi StaticApi = StaticRiotApi.GetInstance(_Key);

                RiotSharp.SummonerEndpoint.Summoner Summoner = Api.GetSummonerByName(_Region, _NombreInvocador);
                List<RiotSharp.SpectatorEndpoint.Participant> Participants = Api.GetCurrentGame(_Region, Summoner.Id).Participants;

                // --- ## Diccionarios para cargar los campeones & hechizos ## --- //

                Dictionary<string, RiotSharp.StaticDataEndpoint.Champion.ChampionStatic>.ValueCollection championlist =
                        StaticApi.GetChampions(_Region, RiotSharp.StaticDataEndpoint.ChampionData.All).Champions.Values;

                Dictionary<string, RiotSharp.StaticDataEndpoint.SummonerSpell.SummonerSpellStatic>.ValueCollection SummonerSpell =
                        StaticApi.GetSummonerSpells(_Region, RiotSharp.StaticDataEndpoint.SummonerSpellData.All).SummonerSpells.Values;

                // --- ## Diccionarios para cargar los campeones & hechizos ## --- //

                int total = Participants.Count; // 10 Jugadores, 6 jugadores, X jugadores.
                int index = Participants.FindIndex(a => a.SummonerName == _NombreInvocador);

                enemigos = new string[total / 2, 3];

                if (index > (total / 2) - 1)
                    for (int i = 0; i <= (total / 2) - 1; i++)

                        CampeonWhere = championlist.Where(yourself => yourself.Id == Participants[i].ChampionId);

                        Spell1Where = SummonerSpell.Where(yourself => yourself.Id == Participants[i].SummonerSpell1);

                        Spell2Where = SummonerSpell.Where(yourself => yourself.Id == Participants[i].SummonerSpell2);

                        enemigos[i, 0] = CampeonWhere.First().Name;
                        enemigos[i, 1] = Spell1Where.First().Name;
                        enemigos[i, 2] = Spell2Where.First().Name;

                    for (int i = 0; i <= (total / 2) - 1; i++)

                        CampeonWhere = championlist.Where(yourself => yourself.Id == Participants[total / 2 + i].ChampionId);

                        Spell1Where = SummonerSpell.Where(yourself => yourself.Id == Participants[total / 2 + i].SummonerSpell1);

                        Spell2Where = SummonerSpell.Where(yourself => yourself.Id == Participants[total / 2 + i].SummonerSpell2);

                        enemigos[i, 0] = CampeonWhere.First().Name;
                        enemigos[i, 1] = Spell1Where.First().Name;
                        enemigos[i, 2] = Spell2Where.First().Name;

            catch (RiotSharpException)
                Console.WriteLine("No funca tio");

In the game, el máximo de jugadores es 10 , 5 rivals 5 allies, the minimum is 2. (1 opponent, 1 ally). I pass the Key provided by the Api (The key is a private field, which previously had to be filled, this method can not be invoked without the fields _Key , _Region and _ NombreInvocador are empty.) and from there I generate a lista with the participantes that are at stake.

These can be X , if they were 10 , cinco primeros belong to one team, and cinco restantes to another. If they were 8 , the cuatro primeros belong to one team and the cuatro restantes to another.

Bearing this in mind, as I want to know only the enemy team, I have to ask myself in what position is the person sought. (The main player, let's say me). If I am in a position less than (NumeroJugadoresTotales/2)-1 , it means that the enemy team is starting from the second half. For example, if there are 10 players, and the position of the main player is number 6, it means that the enemies are from 0 to 4 , because we know that they are taken from 5 en 5 . (As I mentioned before)

Knowing this, and knowing in which position is the rival team, I go through a multidimensional array, (and the length of this, varies depending on the number of total people) If there are 10 people playing, it means that I will only need half ( the enemies).

Each enemy has two base spells, so the multidimensional array has a costante de 2 .

Now, I load the dictionaries of the characters that exist, and the spells that exist, as the method returns the ID in long , what I do is ask within the dictionary to which string corresponds that long.

Imagine that the name of the character played would be Sharki , and the id is 8 . With the method GetChampions you would get the id , and with said id you would ask in the dictionary previously loaded that string corresponds, resulting in Sharki .

So we have a multidimensional array something like that.

  • CharacterName1 [0, 0]
    • SpellPersonaje1 [0, 1]
    • SpellPersonaje2 [0, 2]
  • PersonName2 [1, 0]
    • SpellPersonaje1 [1, 1]
    • SpellPersonaje2 [1, 2]

Etc ...

As it is a void method, and this method is executed in the form2_load , this will load the field enemigos[,] with the data, to later work from there.

So far so good, now comes the problem (and the one that, according to my head, does not see where it is).

I want to upload an image of each character with its respective spell, so I set a array of pictureboxs .

PictureBox[,] imagen = new PictureBox[SharkiQuerys.enemigos.Length / 3, 2 ];

            imagen[0,0] = Invocador1;
            imagen[0,1] = Inv1Spell1;
            imagen[0,2] = Inv1Spell2;

            imagen[1,0] = Invocador2;
            imagen[1,1] = Inv2Spell1;
            imagen[1,2] = Inv2Spell2;

            imagen[2,0] = Invocador3;
            imagen[2,1] = Inv3Spell1;
            imagen[2,2] = Inv3Spell2;

            imagen[3,0] = Invocador4;
            imagen[3,1] = Inv4Spell1;
            imagen[3,2] = Inv4Spell2;

            imagen[4,0] = Invocador5;
            imagen[4,1] = Inv5Spell1;
            imagen[4,2] = Inv5Spell2;

            for (int i = 0; i <= SharkiQuerys.enemigos.Length / 3 ; i++)
                imagen[i,0].ImageLocation =  ""      + SharkiQuerys.enemigos[i, 0] + ".png";
                imagen[i,1].ImageLocation =  "" + SharkiQuerys.enemigos[i, 1] + ".png";
                imagen[i,2].ImageLocation =  "" + SharkiQuerys.enemigos[i, 2] + ".png";


Since the array does not have the property count , I use the property lenght that returns the number of total elements, in this case, and for this example are fifteen. Since the matrix is 5x3 and that is a total of 15 elements.

Since I need to work with an equal matrix and I know that the constant is always 3. (Name, + spell + spell 2) I divide the length by 3, and I continue with the constant of three such that the array remains with [Longitud/3, 3] (remember that the length will vary depending on whether there are 10 playersm 6, X ...)

Now the only thing I need is to go through a for that runs through each picture box to fill the total number of players, with their respective spells.

And here's the problem when executing the program:

The index is exceeded, but the matrix is correct, I tried to do it with constants in the array, so that the matrix was [5, 3] but even with those, the error jumps there.

Doing debuggin (the little I know about it) does not even get to the for, returns error in that position of the matrix and I do not know why.

It would be very helpful if someone could guide me, I hope I have explained everything clearly, if you need more information ask me, thank you.

Edit: I feel silly, the problem is that I declare an array with a constant of 2, when it is 3. That's why it throws the exception ...

Likewise, I am aware that the code could be optimized much more, if someone had the necessary time to explain what things I am doing wrong, I would appreciate it.

asked by Omar 14.08.2018 в 18:58

0 answers