在我的计算行中,我得到错误“使用未分配的局部变量...”,用于提交住宿费,注册费和天数。我已经在开始时声明了变量,所以我不知道我应该做什么。
private void button1_Click(object sender, EventArgs e)
{
decimal days;
decimal registrationFee;
decimal lodgingFee;
decimal total;
decimal lodgingCal;
string registration;
if (workshopListBox.SelectedIndex != -1)
{
registration = workshopListBox.SelectedItem.ToString();
switch (registration)
{
case "Handling Stress":
registrationTextBox.Text = "$1000";
days = 3;
registrationFee = 1000;
break;
case "Time Managment":
registrationTextBox.Text = "$800";
days = 3;
registrationFee = 800;
break;
case "Supervision Skills":
registrationTextBox.Text = "$1500";
days = 3;
registrationFee = 1500;
break;
case "Negotiation":
registrationTextBox.Text = "$1300";
days = 5;
registrationFee = 1300;
break;
case "How to Interview":
registrationTextBox.Text = "$500";
days = 1;
registrationFee = 500;
break;
}
}
string lodging;
if (locationListBox.SelectedIndex != -1)
{
lodging = locationListBox.SelectedItem.ToString();
switch (lodging)
{
case "Austin":
lodgingTextBox.Text = "$150";
lodgingFee = 150;
break;
case "Chicago":
lodgingTextBox.Text = "$225";
lodgingFee = 225;
break;
case "Dallas":
lodgingTextBox.Text = "$175";
lodgingFee = 175;
break;
case "Orlando":
lodgingTextBox.Text = "$300";
lodgingFee = 300;
break;
case "Phoenix":
lodgingTextBox.Text = "$175";
lodgingFee = 175;
break;
case "Raleigh":
lodgingTextBox.Text = "$150";
lodgingFee = 150;
break;
}
}
lodgingCal = lodgingFee * days;
total = registrationFee + lodgingCal;
totalTextBox.Text = total.ToString("c");
}
}
}
答案 0 :(得分:1)
您必须在使用它们之前初始化变量。
decimal days = 0.0;
decimal registrationFee = 0.0;
decimal lodgingFee =0.0;
decimal total = 0.0;
decimal lodgingCal= 0.0;
答案 1 :(得分:1)
在switch语句中添加一个默认大小写,您可以在其中引发异常或为这些变量分配一些错误值。
switch (registration)
{
case "Handling Stress":
registrationTextBox.Text = "$1000";
days = 3;
registrationFee = 1000;
break;
case "Time Managment":
registrationTextBox.Text = "$800";
days = 3;
registrationFee = 800;
break;
case "Supervision Skills":
registrationTextBox.Text = "$1500";
days = 3;
registrationFee = 1500;
break;
case "Negotiation":
registrationTextBox.Text = "$1300";
days = 5;
registrationFee = 1300;
break;
case "How to Interview":
registrationTextBox.Text = "$500";
days = 1;
registrationFee = 500;
break;
default:
throw new ApplicationException("oh no I don't want this");
}
您收到错误是因为代码中可能存在执行路径,即使尚未分配变量,您也已经在使用它们。这只能在您不处理注册的情况下进行,这就是为什么最好在所有switch语句中添加默认值。这是防御性的编程。
答案 2 :(得分:1)
您必须手动初始化局部变量,因为它默认情况下不会初始化。
您应该在使用之前初始化变量lodgingFee
。
你应该看起来像下面的代码。
private void button1_Click(object sender, EventArgs e)
{
decimal days = 0.0;
decimal registrationFee = 0.0;
decimal lodgingFee = 0.0;
decimal total = 0.0;
decimal lodgingCal = 0.0;
string registration = "";
...............
...................
}
答案 3 :(得分:0)
为每个变量分配一些默认值。
private void button1_Click(object sender, EventArgs e)
{
decimal days = 0.0;
decimal registrationFee = 0.0;
decimal lodgingFee = 0.0;
decimal total = 0.0;
decimal lodgingCal = 0.0;
string registration = String.Empty;
//..............
}