473,387 Members | 1,347 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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?
Nov 13 '05 #1
4 2097
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 expression
rInner["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
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" <tu*****@yahoo.com> 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 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 #3
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" <rf******@NO.SPAM> wrote in message news:<uA**************@tk2msftngp13.phx.gbl>...
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
"Tu-Thach" <tu*****@yahoo.com> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: dmbkiwi | last post by:
I am new to this group, and relatively new to python programming, however, have encountered a problem I just cannot solve through reading the documentation, and searching this group on google. I...
3
by: Roy Adams | last post by:
Hello everyone I'm doing a multiple insert from ten text fields. all named color when I submit the from with the text fields it goes to an asp page with the script to do the job and the go to...
3
by: Mark Morton | last post by:
I'm writing an if statement for a UK credit card form validation script. Users who specify that their card is Switch need to enter either the issue number or the 'valid from' date. I'm trying to...
11
by: sathyashrayan | last post by:
Please look at the following code: static char *text = {"th", "st", "nd", "rd"}; char *ordinal_text(int number) { if (((number %= 100) > 9 && number < 20) || (number %= 10) > 3) number =...
7
by: Mike Heard | last post by:
Hi. I have a question about the evaluation of IF statements when the && operator is used. I have been under the assumption that if the first expression in an IF statement is FALSE it will not...
3
by: Robin Thomas | last post by:
I am fairly new to ASP.NET so I think I am missing something fundamental. Anyway, quite often I am pulling data from a database, but then I need to use that data to produce more data. A simple...
2
by: Rajat Katyal | last post by:
Hi: I prepare the statement for execution as follows: PREPARE query(text) as SELECT count(*) FROM transform_customer_billing where inv_no = $1; The problem is Iam not able to execute this...
4
by: alex | last post by:
I am so confused with these three concept,who can explained it?thanks so much? e.g. var f= new Function("x", "y", "return x * y"); function f(x,y){ return x*y } var f=function(x,y){
18
by: dspfun | last post by:
Hi! The words "expression" and "statement" are often used in C99 and C- textbooks, however, I am not sure of the clear defintion of these words with respect to C. Can somebody provide a sharp...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.