Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #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??
  #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^^
================================================== =========
  #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
  #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^^
================================================== =========
  #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.
  #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^^
================================================== =========


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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.