Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am having datatable dtdata and the value inside the column is 1.
In order to avoid a .ToString() I thoughtouf using .Equals

I have tried this

C#
if(dtData.Rows[0]["Column1"].ToString() == "1") -- working

if(dtData.Rows[0]["Column1"].ToString().Equals("1"))  ---  working 


but its not working like this

C#
if(dtData.Rows[0]["Column1"].Equals("1"))  --- not working 

I thought ".equals" wont be checking type, value only right ?

Can any one please clarify With a practical situation like this and not theories ?

Thanks in advance
Posted
Updated 5-Jan-15 1:05am
v5
Comments
Praveen Kumar Upadhyay 5-Jan-15 6:44am    
Is dtData.Rows[0]["Column1"].ToString().Equals("1") working?
What is the value of dtData.Rows[0]["Column1"].ToString().Equals("1") ?
Praveen Kumar Upadhyay 5-Jan-15 7:03am    
Strange, if(dtData.Rows[0]["Column1"].Equals("1")) should work, because Equals check for the content.
sreesankar89 5-Jan-15 7:04am    
ya thats what i thought ?
but its not working
Praveen Kumar Upadhyay 5-Jan-15 7:09am    
I am sorry, but you might doing some silly mistake. Can you please post the exact code that you are checking. As per article also says == fails to check the content comparison sometimes because of type issue, but Equals always does that.
CLilium 5-Jan-15 7:12am    
I think your datetype of column is not string, that's why when you cast it to string and compare it works, but without the cast it doesn't.

This codeproject article explains the differences quite nicely What is the difference between “==” and .Equals()?[^]
 
Share this answer
 
Comments
Thanks7872 5-Jan-15 7:06am    
Yeah....+5..!
Maciej Los 5-Jan-15 7:13am    
+5
C#
if(dtData.Rows[0]["Column1"].Equals(1))


Try if this works, if yes, it was just you comparing string to integer, otherwise problem is elsewhere.

EDIT:
So after some testing this is what I got:
Integer.Equals() will return true even if you try another direct type which is shorter byte-wise, for example Short or Byte, as long as their value is same.
Same goes for Short, if you try to Equal to Byte with same value it returns true, but if you try to Equal to Integer, who is longer it will fail.
But this looks like it doesn't apply when you use value directly.

Demonstration
C#
int i = 1;
short s = 1;
byte b = 1;

Console.WriteLine(i.Equals(1)); // True
Console.WriteLine(s.Equals(1)); // True
Console.WriteLine(b.Equals(1)); // True

Console.WriteLine((1).GetType().ToString()); // System.Int32

// Int.Equals(Short) - Int32.Equals(Int16)
Console.WriteLine(i.Equals(s)); // True

// Short.Equals(Int) - Int16.Equals(Int32)
Console.WriteLine(s.Equals(i)); // False


The problem you have is that Integer.Equals is not same Object.Equals (or ValueType.Equals according to MSDN, not sure which is used though), which will also check for type and as Praveen Kumar Upadhyay commented your dtData.Rows is probably array of objects, after trying out GetType even if it sais it's Int16 or Int32 or whatever it will still use Object.Equals instead of the respected method of the types it sais it is.

Notice I checked what type a number 1 is, it's System.Int32 and now when we also compare type we run into this.

The situation you are in is like this:
C#
Console.WriteLine(((object)i).Equals(1)); // True
Console.WriteLine(((object)s).Equals(1)); // False
Console.WriteLine(((object)b).Equals(1)); // False


And way to fix it is
C#
Console.WriteLine(((object)i).Equals(1)); // True
Console.WriteLine(((object)s).Equals((short)1)); // True
Console.WriteLine(((object)b).Equals((byte)1)); // True
 
Share this answer
 
v2
Comments
sreesankar89 5-Jan-15 7:45am    
if(dtData.Rows[0]["Column1"].Equals(1)) -- not working
sreesankar89 5-Jan-15 7:46am    
dtData.Rows[0]["RequestType"].Equals((byte)1) -- working
i dont know how?
CLilium 5-Jan-15 8:34am    
Can you check and report back your type of dtData.Rows[0]["Column1"], either by debuging or GetType? This is quite important.

As you mentioned in comments above if your column is tinyint, it's most likely cast to short, can you try this:

if(dtData.Rows[0]["Column1"].Equals((short)1))

I have no way to actually check it, but it might be possible that byte equals short might return true as they are same lenght bitwise, I'll be able to check this after few hours if still needed.
As you have mentioned that your column type is tinyint(1). Now when we load the select statement into DataTable, tinyint column value is getting converted into short type of.

Now I will explain your scenario, why
if(dtData.Rows[0]["Column1"].Equals("1"))
returning false.

Consider the below example.

C#
short s = 1;
s.ToString() == "1"

above statement will return true, because when we are doing s.ToString() is returning "1" and we are comparing this with "1".


C#
s.ToString().Equals("1")

above statement will return true, because when we are doing s.ToString() is returning "1" and we are comparing this with "1" using Equals.

C#
s.Equals("1")

above statement will return false, because we are comparing two different types where 1 and Equals("1") are different content.

C#
s.Equals(1)

above statement will return true, because now we are comparing the same type value, means contents are same types where 1 and Equals(1) are same content/value.


Hope this will solve your query of why if(dtData.Rows[0]["Column1"].Equals("1")) --- not working
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900