C#-App Using radio buttons and switch case

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • eeffoc
    New Member
    • Feb 2008
    • 5

    C#-App Using radio buttons and switch case

    I've got a set of six radio buttons inside of a list box and what I'm trying to accomplish is generate a random number all with different ranges (Dice for d&d) depending on what radio button is selected using the switch statement.

    Where I'm having the trouble is knowing what radio button is selected so I can use the switch case.

    I'm sure this application has been done before but I'm just learning and I thought it would be fun to try.

    Here is the portion of the code I'm working with

    private void btnRollDice_Cli ck(object sender, EventArgs e)
    {
    this.diceRollNu mbers.Clear();

    Random randomNumber = new Random();

    int numberOfDice = Convert.ToInt32 (this.textNumbe rOfDice.Text.Tr im());
    int die;

    //Roll the number of dice requested
    for (int i = 0; i < numberOfDice; i++)
    {
    //Roll the selected dice
    for (int g = 0; g < groupBoxDice.Co ntrols.Count; g++)
    {
    RadioButton current = (RadioButton)gr oupBoxDice.Cont rols[g];

    switch (current.Checke d)
    {
    case <??>:
    die = randomNumber.Ne xt(1, 4);
    this.diceRollNu mbers.Add(die);
    break;
    case <??>:
    die = randomNumber.Ne xt(1, 8);
    this.diceRollNu mbers.Add(die);
    break;
    }
    }
    }
    this.FillListBo x(this.diceRoll Numbers);
    }
  • zimdanen
    New Member
    • Feb 2008
    • 6

    #2
    Okay, so, what you're doing is going through all the radio buttons, so you don't want to do a switch statement based on whether or not it's checked. If it's true, then you want to roll depending on which radio button you're on.

    But if you're going to do that, you'll still need to figure out which radio button you're on. You would be better served by a series of if-then statements based on each radio button, for instance:

    If radio1.checked
    {
    roll d4s
    }

    else if radio2.checked
    {
    roll d6s
    }

    etc.

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      As an alternate to if-then control:
      In your for loop where you loop through all your radiobuttons, check to see if it is selected, if it is, roll the die that goes with it, otherwise ignore it and carry on.

      Comment

      • zimdanen
        New Member
        • Feb 2008
        • 6

        #4
        Originally posted by Plater
        As an alternate to if-then control:
        In your for loop where you loop through all your radiobuttons, check to see if it is selected, if it is, roll the die that goes with it, otherwise ignore it and carry on.
        You'd still have to figure out which die is associated with the current radio button; seems that the loop is rather extraneous.

        Comment

        • eeffoc
          New Member
          • Feb 2008
          • 5

          #5
          Thanks for the quick responses,

          I replaced the switch case part with the if and else if and that worked. I just need to find another project to use the switch statement in (the part of the book I'm on ;))

          Thanks again

          if (radio4Sided.Ch ecked)
          {
          die = randomNumber.Ne xt(1, 4);
          this.diceRollNu mbers.Add(die);
          }
          else if (radio8Sided.Ch ecked)
          {
          die = randomNumber.Ne xt(1, 8);
          this.diceRollNu mbers.Add(die);
          }

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Originally posted by zimdanen
            You'd still have to figure out which die is associated with the current radio button; seems that the loop is rather extraneous.
            What if you have 30 radio buttons? Writing an if-then for all 30 by hand would be difficult.
            With the loop you can just check it's selected property.
            That statement does go under the assumption that you can directly detect which dice to roll for any given radio button.

            Comment

            • zimdanen
              New Member
              • Feb 2008
              • 6

              #7
              I know this is just a toy program for you, but you'd probably also be better off having the number-of-dice loop inside the if-then statement, since you don't need to check which radio button you have selected n times - it's always going to be the same one.

              If you want to do this as a switch example, try getting rid of the radio buttons and using a drop down list, then switching on ddlist.Text or ddlist.Selected Index/SelectedItem/SelectedValue.

              Comment

              • eeffoc
                New Member
                • Feb 2008
                • 5

                #8
                Yes, it is a toy program :) I was trying to come up with different projects to practice with. Finding different methods to accomplish the same thing is very helpful for me. I'll try moving the for loop as you suggested.

                Now that I have things working by using if else I'm going to try the drop down list with the switch (although I like the look of the radio buttons better) so I'm still going to try to work them out as well (so I guess my original question is still needing work).

                Comment

                • eeffoc
                  New Member
                  • Feb 2008
                  • 5

                  #9
                  Originally posted by Plater
                  As an alternate to if-then control:
                  In your for loop where you loop through all your radiobuttons, check to see if it is selected, if it is, roll the die that goes with it, otherwise ignore it and carry on.
                  This is the part that I am a bit stuck on.
                  I'm trying to determine what radio button is selected and what to compare it to in the case.

                  Comment

                  Working...