473,387 Members | 1,423 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.

Question about evaluation of IF statements

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 evaluate anything after
the && in the same statement.

I have some code that is similar to this:

PaymentEntity pe = GetPaymentEntity();

if (pe.Type == PaymentType.CreditCard &&
((CreditCardPayment)pe).CreditCard.Number > 0)
{
...
}

PaymentEntity - Custom object representing a payment
PaymentType - Custom enum listing the multiple payment types we accept
CreditCardPayment - Custom object representing a credit card payment
(extends PaymentEntity)

I am having a problem where I get an error that states "Specified cast not
valid" when the code is executed with a payment type other than
PaymentType.CreditCard. It appears that an attempt is made to cast pe as a
CreditCardPayment.

My question is, why is the cast even attempted when the first part of the IF
statement evaluates to false?

Thanks.
Nov 17 '05 #1
7 1868

"Mike Heard" <mi*******@nospam.com> wrote in message
news:DB**********************************@microsof t.com...
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 evaluate anything after
the && in the same statement.

I have some code that is similar to this:

PaymentEntity pe = GetPaymentEntity();

if (pe.Type == PaymentType.CreditCard &&
((CreditCardPayment)pe).CreditCard.Number > 0)
{
...
}

PaymentEntity - Custom object representing a payment
PaymentType - Custom enum listing the multiple payment types we accept
CreditCardPayment - Custom object representing a credit card payment
(extends PaymentEntity)

I am having a problem where I get an error that states "Specified cast not
valid" when the code is executed with a payment type other than
PaymentType.CreditCard. It appears that an attempt is made to cast pe as
a
CreditCardPayment.

My question is, why is the cast even attempted when the first part of the
IF
statement evaluates to false?

Thanks.


I believe the inner parens () are controlling the order of operations. As
the cast is in inner (), that's what is attempted first.
Nov 17 '05 #2
Mike Heard <mi*******@nospam.com> wrote:
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 evaluate anything after
the && in the same statement.
Yes, that assumption is correct.
I have some code that is similar to this:

PaymentEntity pe = GetPaymentEntity();

if (pe.Type == PaymentType.CreditCard &&
((CreditCardPayment)pe).CreditCard.Number > 0)
{
...
}

PaymentEntity - Custom object representing a payment
PaymentType - Custom enum listing the multiple payment types we accept
CreditCardPayment - Custom object representing a credit card payment
(extends PaymentEntity)

I am having a problem where I get an error that states "Specified cast not
valid" when the code is executed with a payment type other than
PaymentType.CreditCard. It appears that an attempt is made to cast pe as a
CreditCardPayment.

My question is, why is the cast even attempted when the first part of the IF
statement evaluates to false?


It sounds to me like your pe.Type value is wrong, because C# certainly
does have short-circuiting (which is what you're talking about).

Why do you have one in the first place? Objects already know what kinds
of type they are, so you can just ask them:

if (pe is CreditCardPayment &&
((CreditCardPayment)pe).CreditCard.Number > 0)

or use as:

CreditCardPayment ccp = pe as CreditCardPayment;
if (ccp != null && ccp.CreditCard.Number > 0)
{
...
}

If this doesn't help, could you post a short but complete program which
demonstrates the problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #3


"Jon Skeet [C# MVP]" wrote:
Mike Heard <mi*******@nospam.com> wrote:
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 evaluate anything after
the && in the same statement.


Yes, that assumption is correct.
I have some code that is similar to this:

PaymentEntity pe = GetPaymentEntity();

if (pe.Type == PaymentType.CreditCard &&
((CreditCardPayment)pe).CreditCard.Number > 0)
{
...
}

PaymentEntity - Custom object representing a payment
PaymentType - Custom enum listing the multiple payment types we accept
CreditCardPayment - Custom object representing a credit card payment
(extends PaymentEntity)

I am having a problem where I get an error that states "Specified cast not
valid" when the code is executed with a payment type other than
PaymentType.CreditCard. It appears that an attempt is made to cast pe as a
CreditCardPayment.

My question is, why is the cast even attempted when the first part of the IF
statement evaluates to false?


It sounds to me like your pe.Type value is wrong, because C# certainly
does have short-circuiting (which is what you're talking about).

Why do you have one in the first place? Objects already know what kinds
of type they are, so you can just ask them:

if (pe is CreditCardPayment &&
((CreditCardPayment)pe).CreditCard.Number > 0)

or use as:

CreditCardPayment ccp = pe as CreditCardPayment;
if (ccp != null && ccp.CreditCard.Number > 0)
{
...
}

If this doesn't help, could you post a short but complete program which
demonstrates the problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Thanks for the reply.

I have stepped into it with the debugger and the pe.Type is set to one of
the other values in the PaymentType enum. Stepping into the statement, the
debugger seems to evaluate the pe.Type == PaymentType.CreditCard first
(because when I step into it I end up in the get method of the Type
property), but then the debugger bombs on the cast.

As far as the other ways you suggested of writing the code, I have changed
the code to keep it from blowing up by just breaking up the single IF
statement into 2 separate IF statements. So the code is working as desired
now, but I wanted to try to find out what was happening with the way it was
written in the first place.

Thanks again for your help.
Nov 17 '05 #4
Mike Heard <mi*******@nospam.com> wrote:
Thanks for the reply.

I have stepped into it with the debugger and the pe.Type is set to one of
the other values in the PaymentType enum. Stepping into the statement, the
debugger seems to evaluate the pe.Type == PaymentType.CreditCard first
(because when I step into it I end up in the get method of the Type
property), but then the debugger bombs on the cast.
I'll believe it when I see it in an example that I can run :)
As far as the other ways you suggested of writing the code, I have changed
the code to keep it from blowing up by just breaking up the single IF
statement into 2 separate IF statements. So the code is working as desired
now, but I wanted to try to find out what was happening with the way it was
written in the first place.


First thing to check is whether you've got & instead of && - that would
bypass short-circuiting.

After that, try to set up a short but complete example, and post it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5
I think you may be confusing compile errors with run-time errors.
VC# will provide a compile time error when you code an untenable cast.

In general, short-circuiting does not short-circuit compile checks.
e.g, in "x && <non-compilable code>" the compiler will still need to stop
and inform you that <non-compilable code> is not correct.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant J#: VB.NET to J# Converter
Clear VB: Cleans up outdated VB.NET code
"Mike Heard" wrote:
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 evaluate anything after
the && in the same statement.

I have some code that is similar to this:

PaymentEntity pe = GetPaymentEntity();

if (pe.Type == PaymentType.CreditCard &&
((CreditCardPayment)pe).CreditCard.Number > 0)
{
...
}

PaymentEntity - Custom object representing a payment
PaymentType - Custom enum listing the multiple payment types we accept
CreditCardPayment - Custom object representing a credit card payment
(extends PaymentEntity)

I am having a problem where I get an error that states "Specified cast not
valid" when the code is executed with a payment type other than
PaymentType.CreditCard. It appears that an attempt is made to cast pe as a
CreditCardPayment.

My question is, why is the cast even attempted when the first part of the IF
statement evaluates to false?

Thanks.

Nov 17 '05 #6
David Anton <Da********@discussions.microsoft.com> wrote:
I think you may be confusing compile errors with run-time errors.
VC# will provide a compile time error when you code an untenable cast.
It sounds like the cast isn't untenable at compile time though.
In general, short-circuiting does not short-circuit compile checks.
e.g, in "x && <non-compilable code>" the compiler will still need to stop
and inform you that <non-compilable code> is not correct.


Indeed - but as the OP has got as far as debugging, I suspect that's
not it. The exception message he gave is also the one given at runtime,
not compile time.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #7
Right - I should have read more closely before replying.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant J#: VB.NET to J# Converter
Clear VB: Cleans up outdated VB.NET code
"Jon Skeet [C# MVP]" wrote:
David Anton <Da********@discussions.microsoft.com> wrote:
I think you may be confusing compile errors with run-time errors.
VC# will provide a compile time error when you code an untenable cast.


It sounds like the cast isn't untenable at compile time though.
In general, short-circuiting does not short-circuit compile checks.
e.g, in "x && <non-compilable code>" the compiler will still need to stop
and inform you that <non-compilable code> is not correct.


Indeed - but as the OP has got as far as debugging, I suspect that's
not it. The exception message he gave is also the one given at runtime,
not compile time.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Nov 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

188
by: Ilias Lazaridis | last post by:
I'm a newcomer to python: - E01: The Java Failure - May Python Helps? http://groups-beta.google.com/group/comp.lang.python/msg/75f0c5c35374f553 - I've download (as suggested) the python...
8
by: Anita | last post by:
Hi All, Can multiple updates on one table using single query generate deadlock ? For example, at the same time, there are 2 users run 2 queries as follows : User1 runs : update tab1 set...
8
by: der | last post by:
Hello all, I've a question about order of evaluations in expressions that have && and || operators in them. The question is: will the evalution go left-to-right, no matter what -- even if the...
3
by: moondaddy | last post by:
I'm looking at some sample code but dont know what to make of it. I see an If block that looks like this: #If bla bla bla then Private function xyz() as Boolean 'bla bla bla End Function...
4
by: SL33PY | last post by:
Hi again, I've been coding in vb .Net on my most recent project and during a debug session I noticed that VB .Net hasn't got lazy evaluation... WHY?! I love lazy evaluation and actually count on...
77
by: berns | last post by:
Hi All, A coworker and I have been debating the 'correct' expectation of evaluation for the phrase a = b = c. Two different versions of GCC ended up compiling this as b = c; a = b and the other...
32
by: silpau | last post by:
hi, i am a bit confused on expression evaluation order in expressions involving unary increment.decrement operators along with binary operators. For example in the following expression x...
3
by: Arun Srinivasan | last post by:
I have a query that has 5 predicates, scanning a table of 400+ million rows and I have built indexes getting help from design advisor and other tools. The cost of that query is really low. But I...
39
by: Boltar | last post by:
Why does C do lazy evaluation for logical boolean operations but not bitwise ones? Ie: the following program prints "1 2" , not "1 1" under gcc main() { int a = 1; int b = 1; 0 && ++a;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.