By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
 454,670 Members | 1,330 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,670 IT Pros & Developers. It's quick & easy.

# Why isn't the If's statement, after the expression, being executed?

 P: n/a I am writing a Windows Forms application and am testing the values of two different columns of two different records, to see if they are equal or not. If they are equal, then it is supposed to assign the value of true to a bool variable. Here is the relevant code snippet: bTmp = false; foreach (DataRow rInner in dtEEICD9.Rows) if (rInner["ICD9Seq"] == r["ICD9Seq"]) bTmp = true; I *know* for a fact that there is a record, in each of the two recordsets, that has the same value for the ICD9Seq column. I *KNOW* for a fact that the expression: rInner["ICD9Seq"] == r["ICD9Seq"] does evaluate to true, when the relevant record in rInner has the same value in the ICD9Seq column that r["ICD9Seq"] has. I have verified this fact by stepping through the code in the debugger and evaluating the expression rInner["ICD9Seq"] == r["ICD9Seq"] to see what it says for each of the records. It is only true once, but it IS true once. So, please, would someone tell me why it never assigns the value of true to the variable bTmp? Nov 13 '05 #1
Share this Question
4 Replies

 P: n/a Try using object.Equals instead of == Tu-Thach -----Original Message-----I am writing a Windows Forms application and am testing the values of twodifferent columns of two different records, to see if they are equal or not.If they are equal, then it is supposed to assign the value of true to a boolvariable. Here is the relevant code snippet:bTmp = false;foreach (DataRow rInner in dtEEICD9.Rows) if (rInner["ICD9Seq"] == r["ICD9Seq"]) bTmp = true;I *know* for a fact that there is a record, in each of the two recordsets,that has the same value for the ICD9Seq column. I *KNOW* for a fact thatthe expression:rInner["ICD9Seq"] == r["ICD9Seq"]does evaluate to true, when the relevant record in rInner has the same valuein the ICD9Seq column that r["ICD9Seq"] has. I have verified this fact bystepping through the code in the debugger and evaluating the expressionrInner["ICD9Seq"] == r["ICD9Seq"]to see what it says for each of the records. It is only true once, but itIS true once. So, please, would someone tell me why it never assigns thevalue of true to the variable bTmp? . Nov 13 '05 #2

 P: n/a Tu-Thach, Thank you for replying to my message! I am interested in whether or not the value of rInner["ICD9Seq"] (let's say it equals 1) is equal to the value of r["ICD9Seq"] (I know that it is 1 at some point). But your answer suggests something to me that I hadn't considered. In this context, does rInner["ICD9Seq"] == r["ICD9Seq"] determine whether or not the two objects are the same, rather than the value of column ICD9Seq of the current record r compared to the value of column ICD9Seq of the current record rInner? Rod "Tu-Thach" wrote in message news:99****************************@phx.gbl... Try using object.Equals instead of == Tu-Thach-----Original Message-----I am writing a Windows Forms application and am testing the values of twodifferent columns of two different records, to see if they are equal or not.If they are equal, then it is supposed to assign the value of true to a boolvariable. Here is the relevant code snippet:bTmp = false;foreach (DataRow rInner in dtEEICD9.Rows) if (rInner["ICD9Seq"] == r["ICD9Seq"]) bTmp = true;I *know* for a fact that there is a record, in each of the two recordsets,that has the same value for the ICD9Seq column. I *KNOW* for a fact thatthe expression:rInner["ICD9Seq"] == r["ICD9Seq"]does evaluate to true, when the relevant record in rInner has the same valuein the ICD9Seq column that r["ICD9Seq"] has. I have verified this fact bystepping through the code in the debugger and evaluating the expressionrInner["ICD9Seq"] == r["ICD9Seq"]to see what it says for each of the records. It is only true once, but itIS true once. So, please, would someone tell me why it never assigns thevalue of true to the variable bTmp? . Nov 13 '05 #3

 P: n/a You can also: if (rInner["ICD9Seq"].ToString() == r["ICD9Seq"].ToString()) bTmp = true; This forces string comparison, and in string comparisons, == is overloaded to compare the value, which is what you're looking to do. And to reduce your code a little, bTmp = (rInner["ICD9Seq"].ToString() == r["ICD9Seq"].ToString()); "Rod" wrote in message news:... I am writing a Windows Forms application and am testing the values of two different columns of two different records, to see if they are equal or not. If they are equal, then it is supposed to assign the value of true to a bool variable. Here is the relevant code snippet: bTmp = false; foreach (DataRow rInner in dtEEICD9.Rows) if (rInner["ICD9Seq"] == r["ICD9Seq"]) bTmp = true; Nov 13 '05 #4

 P: n/a "Tu-Thach" wrote in news:a6f101c346ea\$040ae3e0 \$a*******@phx.gbl: Rod, Remember that the == operator compares object references. If you value is 1 (int) then the two int objects are actually different even though the values are the same. By using object.Equals() method, you compare the values of the objects and not references. I hope this answers your question. Tu-Thach Or you could cast each object to int, then you'd get the overloaded == operator for ints. Mark Nov 13 '05 #5

### This discussion thread is closed

Replies have been disabled for this discussion.