使用 Try、catch、finally 的局部变量未分配问题
Local variable unassigned issue using Try, catch, finally
我想知道我是否可以帮忙。有人可以向我解释为什么我的字符串 sqrt
在 finally
块中未分配吗?为什么我必须申报?为什么不能在try或catch语句中声明呢?这将使编码变得不那么乏味和更有条理。
private void btnDisplay_Click(object sender, EventArgs e)
{
int number;
string sqrt;
try
{
number = Convert.ToInt32(tbInput.Text);
//Why cant i just have it as "number=COnvert.ToDouble(tbInput.Text)?//
Convert.ToDouble(number);
if (number < 0)
{
throw new NegativeNumberException();
}
sqrt = Math.Sqrt(number).ToString();
}
catch (FormatException error)
{
lbOutput.Items.Add(error.Message);
lbOutput.Items.Add("The input should be a number.");
sqrt = "not able to be calculated";
}
catch (NegativeNumberException neg)
{
lbOutput.Items.Add(neg.Message);
sqrt = "not able to be calculated";
}
finally
{
//Here is where i am having the issue "Unassigned local variable"//
lbOutput.Items.Add("Square Root " + sqrt);
}
}
class NegativeNumberException : Exception
{
public NegativeNumberException()
: base("Number can’t be negative")
{
}
}
}
}
我试图在 finally 块中实现的是 "Square Root" 和 "sqrt" 显示在列表框中,无论 sqrt 的值是多少。如果我将 sqrt 输出到任何其他块中的列表框,它就会工作(因为它已被声明)。有谁知道我该怎么做?我敢打赌这可能也很简单。我并不是要咆哮或任何事情,只是我在过去的 12 个小时里一直在起床,所以我开始感到失败。感谢大家的帮助,真的。
不能在try
块中声明,因为局部变量被scope绑定。简而言之,在块中声明的局部变量,即 {}
,仅在该块中可见。补充一下,如果你在声明的时候将sqrt
初始化为""
或string.Empty
会更好。
如果您的代码中有以下任何行:
number = Convert.ToInt32(tbInput.Text);
//Why cant i just have it as "number=COnvert.ToDouble(tbInput.Text)?//
Convert.ToDouble(number);
if (number < 0)
{
throw new NegativeNumberException();
}
抛出一个非 NegativeNumberException
或 FormatException
类型的异常,然后由于这个声明:
string sqrt;
您的 sqrt
变量仍未分配。
你可以这样声明来解决这个问题:
string sqrt = null; // or ""
关于您的评论:
Why cant i just have it as "number=COnvert.ToDouble(tbInput.Text)?
试试这个:
var number = Double.Parse(tbInput.Text);
变化:
int number;
string sqrt;
更新:
double number = 0.0;
string sqrt = string.Empty;
尝试在 sqrt 上赋值。
string sqrt = "";//声明 sqrt 可能不包含任何引发问题的值。
@Corak,在任何块解决问题之前初始化字符串。
我变了
string sqrt;
至
string sqrt=string.Empty;
sqrt 仅在声明的范围内可用。作用域通常由花括号分隔,例如方法主体、for 语句,或者在本例中为 try、catch 和 finally 子句。当尝试在 if 子句中声明一个变量,然后尝试在 else 对应项中使用该变量时,您会注意到同样的问题。如果你有很多这样的东西,并且只在 try 或 catch 子句中声明它,一种替代方法是创建一个全局变量映射,然后在每个范围内将 "sqrt" 键分配给你想要的对象.
我想知道我是否可以帮忙。有人可以向我解释为什么我的字符串 sqrt
在 finally
块中未分配吗?为什么我必须申报?为什么不能在try或catch语句中声明呢?这将使编码变得不那么乏味和更有条理。
private void btnDisplay_Click(object sender, EventArgs e)
{
int number;
string sqrt;
try
{
number = Convert.ToInt32(tbInput.Text);
//Why cant i just have it as "number=COnvert.ToDouble(tbInput.Text)?//
Convert.ToDouble(number);
if (number < 0)
{
throw new NegativeNumberException();
}
sqrt = Math.Sqrt(number).ToString();
}
catch (FormatException error)
{
lbOutput.Items.Add(error.Message);
lbOutput.Items.Add("The input should be a number.");
sqrt = "not able to be calculated";
}
catch (NegativeNumberException neg)
{
lbOutput.Items.Add(neg.Message);
sqrt = "not able to be calculated";
}
finally
{
//Here is where i am having the issue "Unassigned local variable"//
lbOutput.Items.Add("Square Root " + sqrt);
}
}
class NegativeNumberException : Exception
{
public NegativeNumberException()
: base("Number can’t be negative")
{
}
}
}
}
我试图在 finally 块中实现的是 "Square Root" 和 "sqrt" 显示在列表框中,无论 sqrt 的值是多少。如果我将 sqrt 输出到任何其他块中的列表框,它就会工作(因为它已被声明)。有谁知道我该怎么做?我敢打赌这可能也很简单。我并不是要咆哮或任何事情,只是我在过去的 12 个小时里一直在起床,所以我开始感到失败。感谢大家的帮助,真的。
不能在try
块中声明,因为局部变量被scope绑定。简而言之,在块中声明的局部变量,即 {}
,仅在该块中可见。补充一下,如果你在声明的时候将sqrt
初始化为""
或string.Empty
会更好。
如果您的代码中有以下任何行:
number = Convert.ToInt32(tbInput.Text);
//Why cant i just have it as "number=COnvert.ToDouble(tbInput.Text)?//
Convert.ToDouble(number);
if (number < 0)
{
throw new NegativeNumberException();
}
抛出一个非 NegativeNumberException
或 FormatException
类型的异常,然后由于这个声明:
string sqrt;
您的 sqrt
变量仍未分配。
你可以这样声明来解决这个问题:
string sqrt = null; // or ""
关于您的评论:
Why cant i just have it as "number=COnvert.ToDouble(tbInput.Text)?
试试这个:
var number = Double.Parse(tbInput.Text);
变化:
int number;
string sqrt;
更新:
double number = 0.0;
string sqrt = string.Empty;
尝试在 sqrt 上赋值。 string sqrt = "";//声明 sqrt 可能不包含任何引发问题的值。
@Corak,在任何块解决问题之前初始化字符串。
我变了
string sqrt;
至
string sqrt=string.Empty;
sqrt 仅在声明的范围内可用。作用域通常由花括号分隔,例如方法主体、for 语句,或者在本例中为 try、catch 和 finally 子句。当尝试在 if 子句中声明一个变量,然后尝试在 else 对应项中使用该变量时,您会注意到同样的问题。如果你有很多这样的东西,并且只在 try 或 catch 子句中声明它,一种替代方法是创建一个全局变量映射,然后在每个范围内将 "sqrt" 键分配给你想要的对象.