【发布时间】:2012-07-04 19:53:31
【问题描述】:
我想要实现的是,显示以给定字符(通过 textbox1)结尾/开头的行数(用户通过 comboBox1 选择类型)。
尝试编译这段代码:
string needle=textBox1.Text.Trim(), cboxSelection = comboBox1.Text;
int count;
switch (cboxSelection)
{
case "Starting with":
count = File.ReadLines(openFileDialog1.FileName).Count(line => Regex.IsMatch(line, "^" + needle + ".*$"));
break;
case "Ending with":
count = File.ReadLines(openFileDialog1.FileName).Count(line => Regex.IsMatch(line, "^.*" + needle + ".*$"));
break;
}
string strCount = count.ToString(); // error line
label6.Text = "There are " + strCount + " lines " + cboxSelection + " " + needle + " character.";
收到错误消息:Use of unassigned local variable 'count'。我错过了什么?
【问题讨论】:
-
再次阅读错误信息。什么时候会是真的?
-
Use of unassigned local variable 的可能重复项(用于“if”,但与此处的想法完全相同。-1 是因为有太多太接近的匹配项。)
标签: c# visual-studio compiler-errors