Fedora Linux Support Community & Resources Center
  #1  
Old 16th January 2010, 05:02 AM
crainey69 Offline
Registered User
 
Join Date: Oct 2007
Location: Macon, MO
Age: 44
Posts: 330
linuxfirefox
question on a boolean in switch in C#

I'm working on a program for a programming class and from what I can tell, you can't use a boolean expression in a switch/case. I don't need a solution but, rather an answer if it can be done. I want to say.. for example:

case (age <= 12)
do this;
break;

I'm pushed into a Windows world but, run Redhat derivatives at home for the most part and run Windows in a virtual machines. I've googled everything I can think of.

Can anyone at least point me in the rigth direction to find a way to do it?
Reply With Quote
  #2  
Old 16th January 2010, 08:31 PM
SaGS Offline
Registered User
 
Join Date: Nov 2006
Posts: 135
windows_xp_2003opera
You can have an expression in the ‘switch (…)’ itself, but the ‘case’s must be constants or constant expressions. To get what you describe in your example, use an ‘if’, eventually nested like ‘if (…) {…} else if (…) {…} … else {…}’.
Reply With Quote
  #3  
Old 16th January 2010, 08:41 PM
crainey69 Offline
Registered User
 
Join Date: Oct 2007
Location: Macon, MO
Age: 44
Posts: 330
linuxfirefox
Thanks! that's what I was beginning to learn

Quote:
Originally Posted by SaGS View Post
You can have an expression in the ‘switch (…)’ itself, but the ‘case’s must be constants or constant expressions. To get what you describe in your example, use an ‘if’, eventually nested like ‘if (…) {…} else if (…) {…} … else {…}’.
Thanks.. that's what I was finding. Our instructions to modify a program we had written using if elses were a lesson for us on that one. I completed it by using an if statement to set a status then used the status in the switch... totally inefficient but, does what he asked of us.

Apparently, there is a reason why that can't work that is beyond my grasp at the moment because I think it would be very useful.

Thanks

Cory
Reply With Quote
  #4  
Old 6th July 2012, 04:38 PM
Stabzs Offline
Registered User
 
Join Date: Jul 2012
Location: USA
Posts: 1
windows_7firefox
Re: question on a boolean in switch in C#

I know this is a really old post but I did come across it while looking for something similar, so I figured I'd provide an answer while i was here.

Because of the nature of the switch statement, a predetermined value must be given to a switcher variable BEFORE entering the switch. You can work around it like follows:

int caseSwitch = 0;
if (bool1)
{
caseSwitch = 1;
}
else if (bool2)
{
caseSwitch = 2;
}

switch (caseSwitch)
{
case 1:
Console.WriteLine("Case 1");
break;
case 2:
Console.WriteLine("Case 2");
break;
}

However, this is a bit ridiculous since you could then perform your logic in the if/else itself. So the short answer, you do NOT want to use switches in this instance in my opinion. Hopefully it helps anyone else who bumps into it.
Reply With Quote
Reply

Tags
boolean, question, switch

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
problem about a SELinux boolean: allow_user_postgresql_connect basten999 Security and Privacy 0 24th July 2008 08:27 AM
Switch user question ughi Using Fedora 2 28th February 2008 07:02 PM
Options Error: Not a boolean: FXRS Using Fedora 1 30th December 2004 10:19 PM


Current GMT-time: 00:53 (Sunday, 19-05-2013)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletinฎ Copyright ฉ2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat