none
i would like to know how can i compare a switch case 1 and case 2 in C# is it possible to do that? obs i am a begginer :) RRS feed

  • Question

  • i would like to know how can i compare a switch case 1 and case 2 in C# is it possible to do that? obs i am a begginer :) I tried to do it with search and sort but it did not go well

    • Edited by beginer23 Monday, March 9, 2015 10:38 PM
    Monday, March 9, 2015 10:36 PM

Answers

  • You've got a hard-coded string array within the case 2. If you want to act on that in case 1, you'll have to repeat the string array there, or hoist it out of the case statement altogether. The switch can only branch to 1 case per evaluation (unless you build a fall-through case with no break). So you'll need to pull variables common to multiple cases to before the switch statement:

                 //Meny
                 Console.WriteLine("\n HÄNGA GUBBE\n");
                 Console.WriteLine(" 1) Lägg till ord");
                 Console.WriteLine(" 2) Lista alla ord");
                 Console.WriteLine(" 3) Spela");
                 Console.WriteLine(" 4) Avsluta");
                
                string[] array = { " Lev", " Skratta", " Gledje", " Gråt", " Njut" };           
    
                 bool utgång = false;
                 do
                 {
    
                     int Valet;
                     Console.Write("\n\tVälj 1-4: \n ");
                     try
                     {
                         Valet = int.Parse(Console.ReadLine());
                     }
                     catch (Exception)
                     {
                         Console.WriteLine("Du skriver fel character!\n" +
                             "\nSkriv bara mellan 1 och 4");
                         continue;
                     }
    
                     switch (Valet)
                     {
                         case 1:
                             Console.WriteLine("\n lägg ett till ord! ");
                           var input = Console.ReadLine();
                            
                             // Do something with 'array'
    
                             break;
                         case 2:
                             Console.WriteLine("\n Lista med alla ord :\n");
    
                             //string[] array = { " Lev", " Skratta", " Gledje", " Gråt", " Njut" };
                             // Do something with 'array'
                             Array.Sort<string>(array);
    
                             foreach (var c in array)
                                 Console.WriteLine(c);
                             Console.WriteLine("\n");
    
                             break;                  
                     }
                 } while (!utgång);

    Monday, March 9, 2015 11:16 PM
  • i would like to know how can i compare a switch case 1 and case 2 in C# is it possible to do that? obs i am a begginer :) I tried to do it with search and sort but it did not go well

    A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case. Using  switch case statement is select a switch section to execute from a list of candidates.

    You can refer to https://msdn.microsoft.com/en-us/library/06tc147t.aspx

    Based on your description,  you want to compare a switch case 1 and case 2. Am I right? So what do you want to compare? And how to compare?

    My suggestion is please write some C# code with "switch case", You need more practice. It could be help understand more.

    Good luck!

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, March 10, 2015 10:38 AM
    Moderator
  • You might simplify it down to just 2 case statements and translate the strings to English if there is helpful context there.

    Expanding on what Kristin said, only one case block is evaluated per pass through the switch. The case block is the list of statements between the case and the corresponding break. Any data you need to make a decision that's not in that single statement block needs to be stored as variables outside of the switch so that multiple passes through the switch can act on variables stored from a previous iteration of the do loop.

    Tuesday, March 10, 2015 5:48 PM

All replies

  • I don't think I follow the question. Can you give more details? A concrete example?
    Monday, March 9, 2015 10:40 PM
  • let me give you an example if you add a word case 1( lagg ord) how can i compare that word with case 2 words ( in case 2  already exist 5 words)

    here is the my program 

    using System;

    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                //Meny
                Console.WriteLine("\n HÄNGA GUBBE\n");
                Console.WriteLine(" 1) Lägg till ord");
                Console.WriteLine(" 2) Lista alla ord");
                Console.WriteLine(" 3) Spela");
                Console.WriteLine(" 4) Avsluta");

                bool utgång = false;
                do
                {

                    int Valet;
                    Console.Write("\n\tVälj 1-4: \n ");
                    try
                    {
                        Valet = int.Parse(Console.ReadLine());
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("Du skriver fel character!\n" +
                            "\nSkriv bara mellan 1 och 4");
                        continue;
                    }

                    switch (Valet)
                    {
                        case 1:
                            Console.WriteLine("\n lägg ett till ord! ");
                          var input = Console.ReadLine();
                           
                            break;
                        case 2:
                            Console.WriteLine("\n Lista med alla ord :\n");

                            string[] array = { " Lev", " Skratta", " Gledje", " Gråt", " Njut" };
                            Array.Sort<string>(array);

                            foreach (var c in array)
                                Console.WriteLine(c);
                            Console.WriteLine("\n");

                            break;
                        case 3:
                            string guesses, bokstäver;
                            Console.Write("\n Hur många fel får man ha? ");
                            guesses = (Console.ReadLine());
                            Console.WriteLine(" Utmärkt, då spelar vi!\n");
                            Console.WriteLine(" Felgisningar :" + "0/" + guesses);
                            Console.Write(" Gissade bokstäver ");
                            bokstäver = (Console.ReadLine());

                            Console.WriteLine("Aktuellt ord");
                            Console.Write("Gissa bokstav : " + bokstäver + " \n");
                            break;
                        case 4:
                            Console.WriteLine("\n  HEJ DÅ! \n");
                            Console.Beep();
                            utgång = true;
                            break;
                        //avbryter while loopen, avslutar spelet

                    }
                } while (!utgång);
            }
        }
    }

    Monday, March 9, 2015 10:54 PM
  • You've got a hard-coded string array within the case 2. If you want to act on that in case 1, you'll have to repeat the string array there, or hoist it out of the case statement altogether. The switch can only branch to 1 case per evaluation (unless you build a fall-through case with no break). So you'll need to pull variables common to multiple cases to before the switch statement:

                 //Meny
                 Console.WriteLine("\n HÄNGA GUBBE\n");
                 Console.WriteLine(" 1) Lägg till ord");
                 Console.WriteLine(" 2) Lista alla ord");
                 Console.WriteLine(" 3) Spela");
                 Console.WriteLine(" 4) Avsluta");
                
                string[] array = { " Lev", " Skratta", " Gledje", " Gråt", " Njut" };           
    
                 bool utgång = false;
                 do
                 {
    
                     int Valet;
                     Console.Write("\n\tVälj 1-4: \n ");
                     try
                     {
                         Valet = int.Parse(Console.ReadLine());
                     }
                     catch (Exception)
                     {
                         Console.WriteLine("Du skriver fel character!\n" +
                             "\nSkriv bara mellan 1 och 4");
                         continue;
                     }
    
                     switch (Valet)
                     {
                         case 1:
                             Console.WriteLine("\n lägg ett till ord! ");
                           var input = Console.ReadLine();
                            
                             // Do something with 'array'
    
                             break;
                         case 2:
                             Console.WriteLine("\n Lista med alla ord :\n");
    
                             //string[] array = { " Lev", " Skratta", " Gledje", " Gråt", " Njut" };
                             // Do something with 'array'
                             Array.Sort<string>(array);
    
                             foreach (var c in array)
                                 Console.WriteLine(c);
                             Console.WriteLine("\n");
    
                             break;                  
                     }
                 } while (!utgång);

    Monday, March 9, 2015 11:16 PM
  • could you explain little more because i am a beginner

    Tuesday, March 10, 2015 12:49 AM
  • i would like to know how can i compare a switch case 1 and case 2 in C# is it possible to do that? obs i am a begginer :) I tried to do it with search and sort but it did not go well

    A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case. Using  switch case statement is select a switch section to execute from a list of candidates.

    You can refer to https://msdn.microsoft.com/en-us/library/06tc147t.aspx

    Based on your description,  you want to compare a switch case 1 and case 2. Am I right? So what do you want to compare? And how to compare?

    My suggestion is please write some C# code with "switch case", You need more practice. It could be help understand more.

    Good luck!

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, March 10, 2015 10:38 AM
    Moderator
  • You might simplify it down to just 2 case statements and translate the strings to English if there is helpful context there.

    Expanding on what Kristin said, only one case block is evaluated per pass through the switch. The case block is the list of statements between the case and the corresponding break. Any data you need to make a decision that's not in that single statement block needs to be stored as variables outside of the switch so that multiple passes through the switch can act on variables stored from a previous iteration of the do loop.

    Tuesday, March 10, 2015 5:48 PM