0

Here's part of the data from my DataSet:

enter image description here

Take a look at the 16th row's data and the 17 row's data (partno column), the value is "CTS04", but when I do the checking later on, below scenario happens:

enter image description here

The partno column data type is string, and 17 row's data was created manually using below code:

DataRow newRow = ds.Tables[tableToAdd].Rows.Add();
newRow["partno"] = ds.Tables[tableToAdd].Rows[i - 1]["partno"];

So, logically, I'm just trying to copy the partno from the above row, but why when I use "==" operator to check the value, it returns false?

User2012384
  • 4,769
  • 16
  • 70
  • 106
  • Try to use `Trim()` on both sides – NASSER Aug 21 '15 at 11:41
  • 3
    see http://stackoverflow.com/questions/814878/c-sharp-difference-between-and-equals – Mark Aug 21 '15 at 11:44
  • http://www.informit.com/articles/article.aspx?p=2425867 – Mark Aug 21 '15 at 11:44
  • @X-TECH but seems the problem isn't because of the spaces, as you can see in my second screenshot, if I convert the object to string, it returns true.... – User2012384 Aug 21 '15 at 11:44
  • You are talking about 16th and 17th row every where, but the Immediate Window code you showed, you're checking for 15th and 16th. Would you clear the confusion please? – Shaharyar Aug 21 '15 at 11:48
  • @Shaharyar the 15 and 16 in the immediate window is index, so it starts from 0, and I'm talking about the 16th and 17th row of data – User2012384 Aug 21 '15 at 11:49

3 Answers3

3

DataRow[index] returns the content typed as an object. So when you do <object1> == <object2>, the equality comparison of the object type is used, and that will only be true if there is a reference equality between those two objects.

Since your values are actually strings though, you want to use a string based comparison. You can only do that using == if you cast them to strings. Or use .Equals() which is overloaded by the string type to make the comparison:

object a = "foo";
object b = new StringBuilder("foo").ToString(); // this is to make sure that we get a new string object instead of the constant "foo" that is created at compile-time

Console.WriteLine(a == b); // false
Console.WriteLine((string)a == (string)b); // true
Console.WriteLine(a.Equals(b)); // true

For more information see Why would you use String.Equals over ==? and C# difference between == and Equals().

Community
  • 1
  • 1
poke
  • 369,085
  • 72
  • 557
  • 602
2

For objects, the equality operator (==) performs a "ReferenceEquals". In simple types like numers and strings, the operator performs value compare.

ds.Tables[tableToAdd].Rows[i - 1]["partno"] does not return a simple value. try ds.Tables[tableToAdd].Rows[i - 1]["partno"].GetType();

NASSER
  • 5,900
  • 7
  • 38
  • 57
realbart
  • 3,497
  • 1
  • 25
  • 37
0

== comparing the Data Rows not the Value of that rows. Both these rows are different so result is false. In second case it compared value and because value is same the result is true.

Mukund
  • 1,679
  • 1
  • 11
  • 20