继续获取 System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'



我试图制作一个转到网页并输入文本的代码,我对其进行了编码,但是当我加载所需的文本并单击开始时,我得到一个System.IndexOutOfRangeException:"索引超出了数组的边界。 错误。这是我的代码:

for (int index = 0; index < this.Messages.Count; index = checked(index + 1))
{
string[] str = this.Messages[index].Split(new char[]
{
':'
});
using (HttpRequest req = new HttpRequest()) // am using xNet here and below
{
req.UserAgent = Http.ChromeUserAgent();
req.Cookies = new CookieDictionary(false);
req.Proxy = null;
req.IgnoreProtocolErrors = true;
req.AddParam("message-user", str[0]);
req.AddParam("message-wall", str[1]); //this is where the error occurs

有人可以帮我解决这个问题吗?谢谢。

如果任何消息不包含冒号,就会发生这种情况。您需要检查数组的长度,即

req.AddParam("message-wall", str.Length > 1 ? str[1] : String.Empty);

您应该检查是否有":"字符。如果你的 Message[index] 没有任何 ':",你将在数组中只得到一个字符串。 看看这个示例测试,并随意分叉/修改它:https://dotnetfiddle.net/qvwu0U

相关内容

最新更新