Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why does the If statement work in this code snippet?

Status
Not open for further replies.

DoctorWho

MIS
Apr 28, 2003
13
US
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?
 
Try adding curly braces to your statements. Sometimes another statement gets in the way and consumes the statement that you think ought to be running.
Code:
foreach (DataRow rInner in dtEEICD9.Rows)
{
  if (rInner["ICD9Seq"] == r["ICD9Seq"])
  {
    bTmp = true;
  }
}

Chip H.
 
Chip H.,

Thank you for replying to my question!

I tried putting in exactly what you recommended, but it didn't work. It still will not assign the value of true to bTmp. It never executes that line: bTmp = true;

Should I just forget the foreach statement and use a simple for and iterate over dtEEICD9.Rows.Count?
 
You know that you're comparing DataRow objects, and not the values in them, right?

Chip H.
 
Chip H.,

No, I did not know I was comparing DataRow objects! I thought I was comparing the values at the named column. This naturally leads to the question how do I compare the values in the two recordsets for the named columns?

Doctor Who
 
Oops, my mistake. I didn't look closely enough at the definition of the string overloaded indexer for the DataRow class:
Code:
[Serializable]
public object this[
   string columnName
] {get; set;}
[code]
This says it returns an object, and it's type (should) correspond to the type defined for the corresponding DataColumn object.

You might try casting the result to the type you think the row/column is before doing the comparison:
[code]
if (((string)rInner["ICD9Seq"]) == ((string)r["ICD9Seq"]))

Chip H.
 
I think Chip H. is on the right track.

If that doesn't work, try also:

if (rInner["ICD9Seq"].ToString().CompareTo(r["ICD9Seq"].ToString())== 0) { bTmp = true; }

This would apply if the types are strings.
Use CompareTo to compare strings.
 
I found that casting the return object's value to the type I needed (an int, in this case) did the trick.

Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top