【发布时间】:2013-02-28 04:56:12
【问题描述】:
我一直在程序中处理这个字符串代码:
string[] keywords = { "abstract", "as", "etc" };
以及我在这段代码之后使用它的时间(在 mainform.cs 中):
for (int i = 0; i < keywords.Length; i++)
{
if (keywords[i] == token)
{
// Apply alternative color and font to highlight keyword.
rtb.SelectionColor = Color.Blue;
rtb.SelectionFont = new Font("Courier New", 10, FontStyle.Bold);
break;
}
}
但事情是我想为关键字创建单独的类 (KeyWord.cs) 并在 mainform 中声明它,但这段代码不起作用:
KeyWord.cs:
namespace editor
{
class KeyWord
{
string[] keywords = { "abstract", "as", "etc" };
}
}
Mainform.cs:
string[] keywords;
for (int i = 0; i < keywords.Length; i++)
{
if (keywords[i] == token)
{
// Apply alternative color and font to highlight keyword.
rtb.SelectionColor = Color.Blue;
rtb.SelectionFont = new Font("Courier New", 10, FontStyle.Bold);
break;
}
}
错误提示:
使用未分配的局部变量“关键字”:
请注意,此代码在 mainform 中处于无效状态:
private void TextChangedEvent(object sender, EventArgs e)
{
}
我该怎么办?
【问题讨论】:
-
按照编译器的说明初始化变量:
string[] keywords = null; -
@ spajce sir 到 db @ abatishchev sir 的连接字符串仍然存在错误,指向 if (keywords[i] == token) 说 Operator = 不能应用于 'char' 和 ' 类型的操作数string' 和使用未分配的局部变量'keywords'
标签: c# winforms compiler-errors