Welcome to Dream.In.Code
Getting Help is Easy!

Join 132,392 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,252 people online right now. Registration is fast and FREE... Join Now!




checking multiple combinations

 
Reply to this topicStart new topic

checking multiple combinations

Trake
post 29 Aug, 2008 - 04:51 AM
Post #1


D.I.C Head

**
Joined: 29 Jun, 2007
Posts: 60


My Contributions


Hi all,

I want to know if it is possible to check if multiple combinations of values are true for example:
I have 3 boolean values a, b and c andI want to check if a & b are true or a & c are true or b & c are true without doing:

csharp

if (((a == "true") && (b == "true")) || ((a == "true") && (c == "true")) || ((b == "true") && (c == "true")))
{
//do stuff here
}


I know that this example isn't too bad but if there are more than 3 values it starts getting huge (4 is already twice as long as the one above).

Thanks in advance

Ian
User is offlineProfile CardPM

Go to the top of the page

Jayman
post 29 Aug, 2008 - 06:00 AM
Post #2


Student of Life

Group Icon
Joined: 26 Dec, 2005
Posts: 6,839



Thanked 38 times

Dream Kudos: 500

Expert In: C#, VB.NET, Java

My Contributions


Unfortunately, no. However, you can shorten the code. Boolean value comparisons do not require the == "true" part. You can simplify it like the following. Keep in mind that an IF statement evaluates to a boolean answer, as a result you do not need to spell out what it is comparing to when using boolean values.

As you can see it is much shorter and easier to read.
CODE

if ((a && b) || (a && c) || (b && c))
{
}


Hope that helps.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/22/08 07:13AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month