我想要实现的是显示以/开始的行数(用户选择由comboBox1键入的)给定字符(由textbox1)。C#错误:使用“开关”时未分配的本地变量错误?
试图编译这段代码:
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'
。我错过了什么?
再次阅读错误消息。这是真的吗? – 2012-07-04 20:00:38
[使用未分配的局部变量](http://stackoverflow.com/questions/4110251/use-of-unassigned-local-variable)可能的重复(这是“if”,但它与这里完全相同。-1是因为有太多太近的匹配。) – 2012-07-04 20:01:57