【发布时间】:2017-06-08 15:51:16
【问题描述】:
我正在尝试测试列表中的特定元素,并将其与用户输入的字符串进行比较。它在 if 语句中:if (textboxinput.Text.ToString() == main.steps[current])
在这种情况下,current 是一个等于 0 的整数,并且在 if 语句完成 current++ 之后。
这是一个新的 wpf 表单窗口,通过单击 MainWindow wpf 表单上的按钮打开。 main 从包含我们列表的 MainWindow1 类实例化:
public List<string> steps = new List<string>();
每当我运行此代码时,我都会收到参数超出范围异常。
简而言之:我正在尝试用变量替换列表的索引,它说它超出了范围。 仍在学习 c#,这可能是一个简单的修复,这可能是我忘记的一个非常明显的规则,但任何帮助将不胜感激,因为同样的异常也发生在数组上。 这是按钮被点击的事件
int current = 0;
private void buttonanswer_Click(object sender, RoutedEventArgs e)
{
if (textboxinput.Text.ToString() == main.steps[current])
{
textboxoutput.Foreground = Brushes.Green;
textboxoutput.AppendText(textboxinput + Environment.NewLine);
textboxinput.Clear();
}
else
{
textboxoutput.Foreground = Brushes.Red;
textboxoutput.AppendText(textboxinput + Environment.NewLine);
textboxinput.Clear();
}
current++;
}
【问题讨论】:
-
你能展示你实例化 main.steps 数组的代码吗?
-
'MainWindow main = new MainWindow();'这是你要找的吗?我只是使用 main.steps 来调用
-
除非你指的是这个? ` public List
步骤 = new List (); ` -
public List <string> steps = new List<string>();表示main.steps包含零个元素,因此main.steps[current]超出范围。您必须先填充main.steps。 -
感谢您的回答似乎是问题所在!
标签: c# wpf visual-studio list