BitShift

If a variable can be null or have some value, and you want to check the value but also handle the condition when its null, what is a good way to do this without having a bunch of nested if statements Especially when there are a half dozen or more such variables.





Re: Visual C# Language checking for null AND checking the value of a variable

Matthew Watson

Use "&&" in the 'if' statement

if ((myVariable != null) && (myVariable.MyValue == myTarget)
...




Re: Visual C# Language checking for null AND checking the value of a variable

BitShift

Matthew Watson wrote:
Use "&&" in the 'if' statement

if ((myVariable != null) && (myVariable.MyValue == myTarget)
...


But wont that 2nd condition cause an exception if 'myVariable' is null Arent both conditions evaluated





Re: Visual C# Language checking for null AND checking the value of a variable

andypai

Not if you use the && operator. When you use the && operator, it will skip the statement once it hits the 1st incorrect condition ..




Re: Visual C# Language checking for null AND checking the value of a variable

IsshouFuuraibou

Remember your boolean math:
false && true == false
This is true for any number of elements, the moment you have a "false" value, you can't have a true result. This is a fact of boolean math, the language knows this and will stop evaluating the conditions when it finds a false.

The opposite is true with || conditions, it will evaluate until it has a "true" value then not evaluate any more conditions. It doesn't need to for the same reason as false with && conditions





Re: Visual C# Language checking for null AND checking the value of a variable

Matthew Watson

"Boolean short circuiting" as this is known is NOT a fact of boolean math.

It is the way it is implemented on certain languages (including C, C++ and C#), but NOT all languages. Pascal, for example, does NOT do boolean short circuiting.

See http://en.wikipedia.org/wiki/Short-circuit_evaluation for details.




Re: Visual C# Language checking for null AND checking the value of a variable

IsshouFuuraibou

Yes, not all languages allow short circuit, it's a decision of the language definition and whether the language could manage short circuiting well. There are many reason not to short circuit.
This is true especially since boolean math doesn't have to use two state booleans. Also the action of short circuiting is not inherent. However the reason it can short circuit is inherent in logic math, in the link you provided:

Short-circuit evaluation or minimal evaluation denotes the semantics of some boolean operators in some programming languages in which the second argument is only executed or evaluated if the first argument does not suffice to determine the value of the expression: when the first argument of and evaluates to false, the overall value must be false; and when the first argument of or evaluates to true, the overall value must be true. In some programming languages (Lisp), the usual boolean operators are short-circuit. In others (C, Ada), both short-circuit and standard boolean operators are available.


it states pretty clearly: "when the first argument of and evaluates to false, the overall value must be false" this comes from a logic truth table (see the truth table). If this property of logic did not exist, then you couldn't even do short circuiting. That's getting out into the land of logic, which is above and beyond what this question is asking.

Anyways, those interested in more information about this should follow Matthew's link to short-circuit evaluation. If you want to go further that wiki entry links to entries about logic and evaluation. Logic can be very interesting to study.

It is important to note as well that, short circuiting is not bound to happen at only the first condition, it can happen at any condition (tested in C# 1.1):
if ( true && true && false && DoSomething() ) will not execute DoSomething() because of the "false" condition before it.





Re: Visual C# Language checking for null AND checking the value of a variable

boban.s

&& is conditional AND operator and evaluates the second part of statement only if necessary. This operation can be translated as AND Also operator and in VB you will find it as AndAlso opearator.
|| is conditional OR operator and evaluates the second part of statement only if necessary. This operation can be tranlated as OR Else operator and in VB you will find it as OrElse operator.