1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Switch Statements

Discussion in 'C#' started by whirlwind, Apr 15, 2008.

  1. whirlwind

    whirlwind New Member

    Joined:
    Apr 12, 2008
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Hello all,

    I needed your mastery advice again.

    The excercise: Using the Switch statement to write a method that converts the integers 1, 2, 3 and 4 into the words: Diamonds, Hearts, clubs and spades respectively.

    Form design consist of:

    - label text (Suit No.)

    - Text box

    - Button (Convert)

    My codes for above as follow:


    HTML:
            private void button1_Click(object sender, EventArgs e)
            {
                int s;
                s = Convert.ToInt32(textBox3.Text);
                if (s < 1 || s > 4)
                {
                    MessageBox.Show("Please enter a number between 1 and 4");
                }
                string CovertSuit;
                switch (s)
                {
                    case 1:
                        CovertSuit = "Diamond";
                        MessageBox.Show(CovertSuit);
                        break;
                    case 2:
                        CovertSuit = "Heart";
                        MessageBox.Show(CovertSuit);
                        break;
                    case 3:
                        CovertSuit = "Club";
                        MessageBox.Show(CovertSuit);
                        break;
                    case 4:
                        CovertSuit = "Spade";
                        MessageBox.Show(CovertSuit);
                }
            }            
    Receiving the following error when program executed:



    Error 1 Control cannot fall through from one case label ('case 4:') to another



    Please help to point out what I did wrong here



    Thank you so much
     
  2. NewsBot

    NewsBot New Member

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    1
    Trophy Points:
    0
    You need to give a break statement after case 4
     

Share This Page