|
-
Feb 7th, 2011, 09:48 PM
#1
Thread Starter
Hyperactive Member
[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;
}
}
-
Feb 7th, 2011, 11:06 PM
#2
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:
List<string> questions = new List<string>() { "HOW MANY DAYS DID JONAH SPEND IN THE FISH?", "WHAT DID DAVID KILL GOLIATH WITH?", "WHO HAVE SINNED?", "WHAT ARE THE WAGES OF SIN?", // Questions continue down to "WHO WAS THE FATHER OF NOAH?" };
and then in your event handler you do something like:
csharp Code:
int randQ = RandomNumber(1, questions.Count); // Randomize a number from 1 to number of questions label6.Text = "Question #" + randQ.ToString(); qBox.Text = questions[randQ - 1];
-
Feb 8th, 2011, 11:27 AM
#3
Thread Starter
Hyperactive Member
Re: ToString question
Thanks a million. It works fine. Have a great day.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|