【发布时间】:2018-07-29 03:38:00
【问题描述】:
我正在尝试生成多个类对象,但我收到“System.ArgumentOutOfRangeException: 'Index was out of range”。此行的消息:clients[i] = new IRCClient(credentials, textEdit1.Text);
public void FileRead()
{
if (File.Exists(AccountsFile))
{
Account.Clear();
using (StreamReader Reader = new StreamReader(AccountsFile))
{
string line;
while ((line = Reader.ReadLine()) != null)
{
Account.Add(new Accounts { Username = line.Split(':')[0], Password = line.Split(':')[1] });
}
}
}
else
{
File.Create(AccountsFile);
}
}
private void simpleButton1_Click(object sender, EventArgs e)
{
clients = new List<IRCClient>();
int num = 7;
foreach (var acc in Account)
{
for (int i = 0; i < num; i++)
{
credentials = new ConnectionCredentials(acc.Username, acc.Password);
clients[i] = new IRCClient(credentials, textEdit1.Text); //exception thrown
clients.Add(clients[i]);
foreach (var c in clients)
{
c.Connect();
}
}
}
}
【问题讨论】:
-
您似乎误解了列表的工作原理。删除您的索引器(抛出的行),并直接添加对象:
clients.Add(new IRCClient(credentials, textEdit1.Text)); -
尽管如此,您的代码也没有多大意义。真的是要加7倍的账号吗?那个 7 是从哪里来的?
-
@KevinGosse 因为我正在尝试将 7 个机器人从文本文件连接到 IRC 聊天频道,哈哈。
-
嘿@KevinGosse,你认为你可以在不和谐方面帮助我更多吗?