当 int32 行为异常时得到错误答案,我该如何解决? C#
Getting wrong answer when int32 behave wierd, how do I solve it? C#
我正在尝试使用此代码获取结果。它应该可以与我选择的任何 numer/numbers 一起使用,但它最后只能与“0”一起使用。 (val1,是我在其中写入 number/numbers 的文本框)和(yarncombobox,ft3.garnlangd1 是我获取计算所需值的地方)。我猜 Int32 可能是错误的...我应该改用什么?
此外,删除所有数字时我收到错误消息 "System.FormatException: 'Indatasträngen hade ett felaktigt format.'"
private void YarnWeightTextbox_TextChanged(object sender, EventArgs e)
Int32 val1 = Convert.ToInt32(YarnWeightTextbox.Text);
Class2 ft3 = YarnComboBox.SelectedItem as Class2;
{
YarnLengthTextbox.Text = Convert.ToString((val1 / 50) * ft3.garnlangd1);
编辑:我确实让它有点用了,它确实给了正确的浮点值。但同样的问题。删除同一个框中的所有数字时,应用程序崩溃。
float val1 = Convert.ToSingle(YarnWeightTextbox.Text);
Class2 ft3 = YarnComboBox.SelectedItem as Class2;
float val2 = val1 / 50 * ft3.garnlangd1;
{
YarnLengthTextbox.Text = Convert.ToString(val2);
这里有一些建议:
private void YarnWeightTextbox_TextChanged(object sender, EventArgs e)
{
//Check for the empty string. If it is empty then just return
string weight = YarnWeightTextbox.Text;
if(String.IsNullOrEmpty(weight))
{
return;
}
//Otherwise convert to integer or to a float as you have
Int32 val1 = Convert.ToInt32(YarnWeightTextbox.Text);
Class2 ft3 = YarnComboBox.SelectedItem as Class2;
//When casting using the `as` operator. I make a point of checking for null just
//in case for some reason it isn't an instance of the class I expect.
if(ft3 != null)
{
//Finally change the 50 to 50.0. This tells the compiler to divide using a float
//rather than an integer. Dividing 2 integers results in the truncation of the fractional part
YarnLengthTextbox.Text = Convert.ToString((val1 / 50.0) * ft3.garnlangd1);
}
}
最重要的部分是:
- 检查空字符串
- 使用浮点除法而不是整数除法进行除法,因为整数除法会删除结果的小数部分,这可能是您看到的一些奇怪结果的原因。
private void YarnWeightTextbox_TextChanged(object sender, EventArgs e)
{
if (sender is TextBox textBox)
{
if (float.TryParse(textBox.Text, out var yarnWeight) && YarnComboBox.SelectedItem is Class2 class2)
{
var yarnLength = (yarnWeight / 50.0) * class2.garnlangd1;
YarnLengthTextbox.Text = yarnLength.ToString();
}
else
{
YarnLengthTextbox.Text = string.Empty;
}
}
}
正如 Shane Haw 在评论中提到的,您会得到 FormatException
,因为 TextChanged
事件会触发 任何时候文本更改。
如果 YarnWeightTextBox.Text
中的值为 "5"
,并且您删除了 "5"
,则 TextChanged
事件会触发。这意味着您将空字符串 ""
传递给 Convert.ToSingle
。空字符串不会转换为 float
,因此您会得到 FormatException
。如果你在 TextBox
中输入数字以外的任何内容,你也会得到同样的结果。
更好的选择是检查 YarnWeightTextBox.Text
中的值是否解析为 float
而不是使用 Convert.ToFloat
.
private void YarnWeightTextBox_TextChanged(object sender, EventArgs e)
{
float val1 = 0.0F;
if (!float.TryParse(YarnWeightTextBox.Text, out val1))
{
// The value in YarnWeightTextBox.Text does not parse to a float. You could
// do something here to indicate to the user that they did not provide a correct
// value, or just return.
return;
}
// Calculate the yarn length and put it in the YarnLengthTextBox.Text like you're doing now.
}
或者,如果这是 Windows Forms
,您可以查看 NumericUpDown
控件,它会阻止用户输入无效值。
我正在尝试使用此代码获取结果。它应该可以与我选择的任何 numer/numbers 一起使用,但它最后只能与“0”一起使用。 (val1,是我在其中写入 number/numbers 的文本框)和(yarncombobox,ft3.garnlangd1 是我获取计算所需值的地方)。我猜 Int32 可能是错误的...我应该改用什么?
此外,删除所有数字时我收到错误消息 "System.FormatException: 'Indatasträngen hade ett felaktigt format.'"
private void YarnWeightTextbox_TextChanged(object sender, EventArgs e)
Int32 val1 = Convert.ToInt32(YarnWeightTextbox.Text);
Class2 ft3 = YarnComboBox.SelectedItem as Class2;
{
YarnLengthTextbox.Text = Convert.ToString((val1 / 50) * ft3.garnlangd1);
编辑:我确实让它有点用了,它确实给了正确的浮点值。但同样的问题。删除同一个框中的所有数字时,应用程序崩溃。
float val1 = Convert.ToSingle(YarnWeightTextbox.Text);
Class2 ft3 = YarnComboBox.SelectedItem as Class2;
float val2 = val1 / 50 * ft3.garnlangd1;
{
YarnLengthTextbox.Text = Convert.ToString(val2);
这里有一些建议:
private void YarnWeightTextbox_TextChanged(object sender, EventArgs e)
{
//Check for the empty string. If it is empty then just return
string weight = YarnWeightTextbox.Text;
if(String.IsNullOrEmpty(weight))
{
return;
}
//Otherwise convert to integer or to a float as you have
Int32 val1 = Convert.ToInt32(YarnWeightTextbox.Text);
Class2 ft3 = YarnComboBox.SelectedItem as Class2;
//When casting using the `as` operator. I make a point of checking for null just
//in case for some reason it isn't an instance of the class I expect.
if(ft3 != null)
{
//Finally change the 50 to 50.0. This tells the compiler to divide using a float
//rather than an integer. Dividing 2 integers results in the truncation of the fractional part
YarnLengthTextbox.Text = Convert.ToString((val1 / 50.0) * ft3.garnlangd1);
}
}
最重要的部分是:
- 检查空字符串
- 使用浮点除法而不是整数除法进行除法,因为整数除法会删除结果的小数部分,这可能是您看到的一些奇怪结果的原因。
private void YarnWeightTextbox_TextChanged(object sender, EventArgs e)
{
if (sender is TextBox textBox)
{
if (float.TryParse(textBox.Text, out var yarnWeight) && YarnComboBox.SelectedItem is Class2 class2)
{
var yarnLength = (yarnWeight / 50.0) * class2.garnlangd1;
YarnLengthTextbox.Text = yarnLength.ToString();
}
else
{
YarnLengthTextbox.Text = string.Empty;
}
}
}
正如 Shane Haw 在评论中提到的,您会得到 FormatException
,因为 TextChanged
事件会触发 任何时候文本更改。
如果 YarnWeightTextBox.Text
中的值为 "5"
,并且您删除了 "5"
,则 TextChanged
事件会触发。这意味着您将空字符串 ""
传递给 Convert.ToSingle
。空字符串不会转换为 float
,因此您会得到 FormatException
。如果你在 TextBox
中输入数字以外的任何内容,你也会得到同样的结果。
更好的选择是检查 YarnWeightTextBox.Text
中的值是否解析为 float
而不是使用 Convert.ToFloat
.
private void YarnWeightTextBox_TextChanged(object sender, EventArgs e)
{
float val1 = 0.0F;
if (!float.TryParse(YarnWeightTextBox.Text, out val1))
{
// The value in YarnWeightTextBox.Text does not parse to a float. You could
// do something here to indicate to the user that they did not provide a correct
// value, or just return.
return;
}
// Calculate the yarn length and put it in the YarnLengthTextBox.Text like you're doing now.
}
或者,如果这是 Windows Forms
,您可以查看 NumericUpDown
控件,它会阻止用户输入无效值。