Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 23rd, 2007, 04:27 PM
Authorized User
 
Join Date: Jan 2007
Location: , LA, .
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to seymour_glass
Default switch statement

ok so I'm a little confused about this. What chooses the cases? I am writing a simple console app that displays an output based on a entered variable (in this case, a letter grade i.e. A, B, C etc) so should it look like:

switch
{
    case A: code

    case B: code

    case C: code
}
??
Do A, B, and C need to be declared first??
Reply With Quote
  #2 (permalink)  
Old January 23rd, 2007, 04:38 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

No, its just like a SELECT CASE in VB so you need to do this:

string value = "something";

switch(value)
{
   case "something":
      //code
      break;
   case "Something Else":
     //code
     break;
}

remember to put your break statements in there or the compiler will bark at you about code does not support fall through.

================================================== =========
I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
================================================== =========
Reply With Quote
  #3 (permalink)  
Old January 23rd, 2007, 06:35 PM
Authorized User
 
Join Date: Jan 2007
Location: , LA, .
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to seymour_glass
Default

OK i dont understand what string i need. Here however is the code i have started.....

Seymour
Code:
{
            char charGrade;

            Console.WriteLine("Enter the letter grade to see your student status");
            Console.Write("Please enter your selection: ");
            Console.ReadLine(charGrade);
            char.Parse(charGrade);


            switch (charGrade)
            {
                case A: Console.WriteLine("Honor student");
                        break;

                case B: Console.WriteLine("Honor student");
                        break;

                case C: Console.WriteLine("Average student");
                        break;

                case D: Console.WriteLine("Below average student");
                    break;

                case F: Console.WriteLine("Below average student");
                    break;

                default: 
                    Console.WriteLine("Invalid selection. Please select an A,B,C,D or F.");            
                    break;
Seymour
Reply With Quote
  #4 (permalink)  
Old January 23rd, 2007, 06:42 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Is this code compiling? I am going to say no and that is probably barking about variable A B C D and F arent defined. Enclose those letters in " "

================================================== =========
I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
================================================== =========
Reply With Quote
  #5 (permalink)  
Old January 23rd, 2007, 07:26 PM
Authorized User
 
Join Date: Jan 2007
Location: , LA, .
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to seymour_glass
Default

Ya I was getting those build errors along with some others that i think i fixed. Anyways. In reading the input, what argument do i need? Also, Im still a little stuck on what my argument needs to be for my switch statement. The compiler doesnt like

switch (strGrade)

I would like my input to only read one character (i.e. A B or C )rather than a string, but when i try to parse the input into charGrade the compiler is bumping into problems converting strings to chars???

Sry for the trouble, its been about 3 semesters since i did the first part of this course and im trrying to get my head back in it.
Reply With Quote
  #6 (permalink)  
Old January 24th, 2007, 12:08 AM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Instead of this:
Console.ReadLine(charGrade);
char.Parse(charGrade);

You may want to try
charGrade = Console.ReadLine();

here is the problem I for see you having with your code. A char represents one character but, what happens if a user types in AB? Your code is going to throw an exception =\ I would suggest using a string unless you are going to write in error checking/handling so that if more then one char is passed in, you pass only one char to your variable.

================================================== =========
I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
================================================== =========
Reply With Quote
Reply


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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
switch statement crmpicco Javascript How-To 3 March 9th, 2015 10:37 PM
Switch-statement grstad C# 3 June 11th, 2008 06:53 AM
Using Switch statement in XHTML Lizane Java Basics 1 June 15th, 2007 06:46 AM
C# switch statement nalla General .NET 4 December 14th, 2005 05:31 AM
build switch statement to make dynamic crmpicco Javascript How-To 0 October 27th, 2005 05:44 AM



All times are GMT -4. The time now is 05:03 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.