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

R

Rod

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?
 
R

Rod

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 said:
Try using object.Equals instead of ==

Tu-Thach
-----Original Message-----
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?


.
 
C

Chris Hornberger

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());
 
M

Mark

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top