Array declared:

public static string[] topUsers = new string[65536]; 

And this code works with it:

 public async void GetGlobalTop() { gtlist.Items.Clear(); string answer = await Consts.URL_GETTOP.PostUrlEncodedAsync(new { accountID = accountID, type = "relative", gjp = gjp }).ReceiveString(); if (answer != "") { Regex reg = new Regex("|"); if (reg.IsMatch(answer)) { string[] users = answer.Split(new char[] { '|' }); int i = 0; foreach (string user in users) { string[] data = user.Split(new char[] { ':' }); Temp.topUsers[i] = data[1] + ":" + data[21]; i++; if (data[1] != "") { gtlist.Items.Add((string)data[1]); } } } else { string[] data = answer.Split(new char[] { ':' }); Temp.topUsers[0] = data[1] + ":" + data[21]; if (data[1] != "") { gtlist.Items.Add((string)data[1]); } } } else { Msg.ShowErrMessage("Ошибка", Consts.ERR); } 

An exception appears on this line:

 Temp.topUsers[i] = data[1] + ":" + data[21]; 

What could be the problem?

  • The index goes beyond the boundary of the array. - CO - Pavel Mayorov
  • @PavelMayorov however, recorded a total of 129 - Rupppppppet
  • What is 129 recorded? - Pavel Mayorov
  • Learn to use the debugger. See what is in the topUsers and data arrays when an exception occurs. - Pavel Mayorov
  • @PavelMayorov I seem to understand the problem in i ++, because i will be more than possible to write to the array - Rupppppppet

1 answer 1

The exception is fixed as follows:

 string answer = answ.Remove(answ.IndexOf('\t'), 3); Regex reg = new Regex("|"); if (reg.IsMatch(answer)) { string[] users = answer.Split(new char[] { '|' }); int i = 0; foreach (string user in users) { if (user == "") { return; } string[] data = user.Split(new char[] { ':' }); Temp.topUsers[i] = data[1] + ":" + data[21]; i++; if (data[1] != "") { gtlist.Items.Add((string)data[1]); } } }