代码正在运行,但显示错误 [英] Code is working but showing an error
本文介绍了代码正在运行,但显示错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码在下面
My Code is below
private void label94_Click(object sender, EventArgs e)
{
float total = 0;
int counter;
for (counter = 0; counter < (dataGridView2.Rows.Count); counter++)
{
if (dataGridView2.Rows[counter].Cells["Price"].Value != null)
{
if (dataGridView2.Rows[counter].Cells["Price"].Value.ToString().Length != null) {
total += float.Parse(dataGridView2.Rows[counter].Cells["Price"].Value.ToString());
}
}
}
textBox1.Text = total.ToString();
}
这段代码运行正常但是通过按住粗体文本代码上的光标显示错误。
错误消息: - 表达式的结果总是'true',因为'int'类型的值永远不会等于'int'类型的'null'。
我不知道为什么这个消息是展示。请有人帮忙。
this code is running properly but showing an error by holding on the cursor on bold text code.
Error Message :- The result of the expression is always 'true' since a value of type 'int' is never equal to 'null' of type 'int?'.
I don't know why this message is showing. Please someone help.
推荐答案
.ToString()返回一个字符串。
String.Length返回一个整数。
值类型永远不会等于null。所以它就像检查0 == null一样,它永远不会。
如果你想检查列是否为空,你必须在打电话之前这样做.ToString,因为当字段中的空值在尝试调用空的.ToString时会导致空引用异常。
.ToString() returns a string.
String.Length returns an integer.
Value types will never equal null. So its like checking if 0 == null which it never will.
If you want to check if the column is null, you have to do it WAY before you call .ToString, since a null value in the field will cause a null reference exception when it tries to call .ToString on a null.
if (dataGridView2.Rows[counter].Cells["Price"].Value != null &&
dataGridView2.Rows[counter].Cells["Price"].Value.ToString().Length > 0)
这篇关于代码正在运行,但显示错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文