Results 1 to 3 of 3

Thread: [RESOLVED] ToString question

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2002
    Location
    Tennessee
    Posts
    378

    Resolved [RESOLVED] ToString question

    Hi,

    I'm trying to get this code to give me the text value of the randmized number.
    If the random number is 3, I want it to ask question q3. It is giving me just the random number in the qBox.Text and not the question itself. The answer is more than likely simple and I'm missing it right in my face.


    Code:
      public partial class BSQ1 : Form
        {
            public BSQ1()
            {
                InitializeComponent();
            }
            private int RandomNumber(int min, int max)
            {
                Random random = new Random();
                return random.Next(min, max);
            }
            string PickedQ;
    
            string q1 = "HOW MANY DAYS DID JONAH SPEND IN THE FISH?";
            string q2 = "WHAT DID DAVID KILL GOLIATH WITH?";
            string q3 = "WHO HAVE SINNED?";
            string q4 = "WHAT ARE THE WAGES OF SIN?";
                // Questions continue down to 
            string q1212 = "WHO WAS THE FATHER OF NOAH?";
    
            private void button1_Click(object sender, EventArgs e)
            {
                this.Close();
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                qBox.Text = "";
                ans1Box.Text = "";
                ans2Box.Text = "";
                ans3Box.Text = "";
                ans4Box.Text = "";
    
                ans1Box.Checked = false;
                ans2Box.Checked = false;
                ans3Box.Checked = false;
                ans4Box.Checked = false;
                            
                int randQ = RandomNumber(1, 1212);  // Randomize a number from 1 to 1,212
    
                label6.Text = "Question #" + randQ.ToString();
                              
                PickedQ ="q" + randQ;
                PickedQ.ToString();
                qBox.Text = PickedQ;            
                  
            }
        }
    Thanks,
    GARY

  2. #2
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: ToString question

    You should place all your strings in an indexed collection, such as List<string>

    Then, you can select a random number from 0 to the collection's Count minus 1, which gives you an index you can use to access the value in the collection.

    csharp Code:
    1. List<string> questions = new List<string>()
    2. {
    3.     "HOW MANY DAYS DID JONAH SPEND IN THE FISH?",
    4.     "WHAT DID DAVID KILL GOLIATH WITH?",
    5.     "WHO HAVE SINNED?",
    6.     "WHAT ARE THE WAGES OF SIN?",
    7.     // Questions continue down to
    8.     "WHO WAS THE FATHER OF NOAH?"
    9. };

    and then in your event handler you do something like:

    csharp Code:
    1. int randQ = RandomNumber(1, questions.Count);  // Randomize a number from 1 to number of questions
    2.  
    3. label6.Text = "Question #" + randQ.ToString();
    4. qBox.Text = questions[randQ - 1];

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2002
    Location
    Tennessee
    Posts
    378

    Re: ToString question

    Thanks a million. It works fine. Have a great day.
    Thanks,
    GARY

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width