Comparing expressions of an object of type

Well, this is probably very simple, but I have the following “checks” (not at the same time) , and the first ALWAYS evaluates to TRUE, and the second SEEMS - to work. It actually happens in every place that the string value is a number or bool (Date seems fine...) .

If I go through the code in Debug, it shows the value of row["PersonID"] as 162434 , the same as tbxPersonID.EditValue . Is this just the basic and novelty of the programming truth I missed in my hodge-podge-self-education ?

It seems that if I included everything in question in a string , at first I will be fine, I just wanted to know if I am correct and if there is a general rule regarding what Types I will need to do this for?

Does not work

 if (row["PersonID"] != tbxPersonID.EditValue) { row["PersonID"] = tbxPersonID.EditValue; } if (row["CitizenFlag"] != chkCitizen.EditValue) { row["CitizenFlag"] = chkCitizen.EditValue; _whatChanged.Add("CitizenFlag"); } 

Work

  if (row["PersonID"].ToString() != tbxPersonID.EditValue.ToString()) { row["PersonID"] = tbxPersonID.EditValue; } if (row["CitizenFlag"].ToString() != chkCitizen.EditValue.ToString()) { row["CitizenFlag"] = chkCitizen.EditValue; _whatChanged.Add("CitizenFlag"); } 
+6
c # types typechecking
source share
6 answers

row["PersonID"] has an object of type, which means that! = and == will use the reference identifier. Basically you are comparing values ​​in a box.

If you use:

 if (!object.Equals(row["PersonID"], tbxPersonID.EditValue)) 

then you get the semantics of equality of values, and I suspect that everything will be fine with you - assuming that tbxPersonID is really int , either in the box or not.

Just to do specific things, here is a short but complete example to show what I'm talking about:

 using System; class Test { static void Main() { object first = 2; object second = 2; // Compares reference equality: false Console.WriteLine(first == second); // Compares value equality: true Console.WriteLine(object.Equals(first, second)); } } 
+18
source share

What is the type of EditValue or the value of the right hand? Probably the problem is that EditValue is not of type string (maybe its subclass or something else), so when you do! = Or == on it, it does a memory address comparison instead of a comparison for a string, so instead of this you get false truths

+2
source share

I don't know about C #, but in some languages ​​equality checking is different for numeric values ​​and strings. Therefore, here you must force a string comparison before it works.

Is this something telling us?

+1
source share

Without calling ToString() your code checks for equality on two objects, not two numbers, so the reason for it doesn't work.

Using ToString() , you explicitly specify the code to get values ​​from the row[] and EditValue objects.

+1
source share

I am assuming that the data type of the row [] object is something like int (or bool), and chkCitizen.EditValue has a different data type. That way you can compare "1" == 1 and have the result false.

0
source share

"12345"! = 12345

0
source share

All Articles