这怎么能不相等呢?
- 我的目标:从 Excel
Range
中提取一个值,并验证这些单元格的值在此范围内是否相同; - 当一个单元格的值与另一个单元格的值不同时,我需要返回 null。
这是一段代码:
internal object GetValueFromCells(string start, string end, Formats format) {
// Verifying for empty or null parameters here and throwing accordingly...
try {
Range cells = Excel.get_Range(start, end) as Range;
object value = null;
bool sameValue = false;
foreach(Range cell in cells) {
// This condition block shall execute only once, since 'value' shall not be null afterwards.
if (value == null || value == DBNull.Value)
if (Formats.Formated == format) {
value = cell.Text;
// The following results to be false !?...
sameValue = value == cell.Text; // Shall this not be true?
} else {
value = cell.Value2;
// The following results to be false !?...
sameValue = value == cell.Value2; // Shall this not be true?
}
// This results being always false!?...
// Shall this not be true, I wonder?
sameValue = Formats.Formated == format ? value == cell.Text : value == cell.Value2;
if(!sameValue)
return null;
}
return value;
} catch (Exception ex) {
// Exception handling...
}
}
阅读这段代码,我谦虚地希望当范围内的所有单元格都具有相同的值(例如 334)时返回一个值。
但是,此方法始终返回 null(在 Visual Basic 中为 Nothing)!
任何人都可以解释我在这里缺少的东西:
value == cell.Value2
总是返回false?
也许是我的算法不太正确?
编辑 #1
这已经解决了问题:
sameValue = Formats.Formatted == format ? cell.Text.Equals(value) : cell.Value2.Equals(value);
我接受了 @Jerod Houghtelling 的答案,因为他的答案建议使用 ToString() 和 Equals() 方法来解决问题。
除此之外,我不喜欢调用 ToString() 方法,因为该值可以是数字,并且比较字符串下的数字对我来说看起来很奇怪。所以我更喜欢在解决方案中采用的 Equals() 方式。
我要感谢@Sir Gallahad 和@Jerod Houghtelling 的良好回答。这是我第一次面对这样的情况,他们都帮助我更好地了解幕后发生的事情,还有其他人也通过评论做出了贡献。
感谢那些对我的问题投赞成票的人。这样做的目的是为了证明我的问题并不是那么愚蠢! =P 呵呵...
- My Goal: Extracting one value from an Excel
Range
, and verify for these cells' value to be the same within this range; - When a cell's value is not the same as the other, I need to return null.
Here's a piece of code:
internal object GetValueFromCells(string start, string end, Formats format) {
// Verifying for empty or null parameters here and throwing accordingly...
try {
Range cells = Excel.get_Range(start, end) as Range;
object value = null;
bool sameValue = false;
foreach(Range cell in cells) {
// This condition block shall execute only once, since 'value' shall not be null afterwards.
if (value == null || value == DBNull.Value)
if (Formats.Formated == format) {
value = cell.Text;
// The following results to be false !?...
sameValue = value == cell.Text; // Shall this not be true?
} else {
value = cell.Value2;
// The following results to be false !?...
sameValue = value == cell.Value2; // Shall this not be true?
}
// This results being always false!?...
// Shall this not be true, I wonder?
sameValue = Formats.Formated == format ? value == cell.Text : value == cell.Value2;
if(!sameValue)
return null;
}
return value;
} catch (Exception ex) {
// Exception handling...
}
}
Reading this code, I would humbly expect a value to be returned when all of the cells in the range have the same value (for instance 334).
However, this methods always returns null (Nothing in Visual Basic)!
Anyone might explain what I'm missing here while this:
value == cell.Value2
always returns false?
Perhaps is it my algorithm that isn't quite right?
EDIT #1
This has solved the problem:
sameValue = Formats.Formatted == format ? cell.Text.Equals(value) : cell.Value2.Equals(value);
I accepted @Jerod Houghtelling's answer as his answer suggests both the ToString() and the Equals() methods to solve the problem.
In addition to it, I dislike having to call the ToString() method, since the value can be numbers, and comparing numbers under a string looks odd to me. So I prefer the Equals() way which I adopted within my solution.
I would like to thank @Sir Gallahad and @Jerod Houghtelling for their good answers. This was the first time I had to face such a situation, and they both helped me better understand what was going on under the hood, plus the others who contributed too through comments.
And thanks to those who upvoted my question. This serves a purpose to demonstrate that I was not so dumb asking! =P Hehehe...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我猜测每次调用
cell.Value2
时都会返回一个对象的新实例。因此我会推断==
正在检查等式两边是否是对象的同一实例。要实际比较两侧存储的值,您必须使用.Equals
或将值转换为可以比较的内容,例如字符串。此外,我没有看到您的示例中设置了value
。I'm guessing that
cell.Value2
is returning a new instance of an object each time you call it. Therefore I would deduce the==
is checking to see if both sides of the equation are the same instance of the object. To actually compare the value stored on both side you will have to use the.Equals
or convert the values to something that can be compared, for example a string.Also I don't seevalue
being set in your example.可能
value == cell.Value2
正在比较来自不同实例的对象。尝试 value.ToString() == cell.Value2.ToString()
Probably the
value == cell.Value2
are comparing objects that are from different instances.Try
value.ToString() == cell.Value2.ToString()