0

I have 10 strings, all named q1,q2,q3, etc.

My question is, on button click, how do I make them cycle and display within a button?

Current code:

private void nButton_Click(object sender, EventArgs e)
    {
        for (int g = 0; g <= 10; g++)
        {
            rBox.Text = q(g);
        }
    }

Clearly q(g) does not cycle appropriately, so I have come to you, Oracles of code, how would I accomplish this?

** Alternatively, if I wanted to remove the for loop, and instead would just want to increment g by one every time until 10, I assume the structure would resemble something like the following:

private void nButton_Click(object sender, EventArgs e)
    {
        g++
            rBox.Text = q(g);       
    } 

However the question persists, how would I cycle through these strings?

EDIT: I've discovered these neat things called Lists, so I simply created a new list with

        List<string> questionNumber = new List<string>();

Then add the string

questionNumber.Add(q1);

As lastly display it through the text box with simple incrementation

private void nButton_Click(object sender, EventArgs e)
    {
        g++;

            rBox.Text = questionNumber[g];

    }
12
  • it should be q[g] not parentheses assuming you are using an array or list to hold your strings. if q is not a list it should be. Commented Oct 24, 2014 at 20:48
  • what is q? Is it a list or a method? Commented Oct 24, 2014 at 20:49
  • 2
    Do you have separate variables named q1, q2, etc? If you do there is no way to create a loop and loop through them. You would have to use an array, a list, or any other enumerable set. Commented Oct 24, 2014 at 20:50
  • 1
    @entropic yes there is way using reflection but indeed not a good one. Commented Oct 24, 2014 at 20:53
  • 1
    @Selman22 True, but like you said it's not a good idea, so why bring it up? :) Commented Oct 24, 2014 at 20:55

1 Answer 1

3

The easiest way would be putting them into an array and iterate over the array whenever you wanna operate on your strings.For example:

var values = new [] { q1, q2, q3, ... };

for (int g = 0; g < 10; g++)
{
   rBox.Text += values[g];
}

If your intention was to display one string at a time, on each click you can do so by creating a counter variable outside of the click event and increment it per click and just fecth the string at that index:

int index = 0;
private void nButton_Click(object sender, EventArgs e)
{
    if(index != values.Length) 
    {
       rBox.Text = values[index];
       index++;
    }    
} 

You need to declare values a field or property of your class, and initialize it with your strings.In fact you can completely remove the variables and just use an array or list to store your values.

Sign up to request clarification or add additional context in comments.

5 Comments

This won't work any better even if it addresses whatever q(g) means. The cycle needs to be maintained outside the button click, or handled differently for animations.
This will give values from q1 to qn concatenated. Was that what he was asking for?
this is just an example, that can be easily achieved by making the array a field or property
@RezaShirazian well overwriting the value on each iteration doesn't make sense so I took a shoot. maybe he wants to display one string at a time but I'm not sure.
This lead me to use Lists I've edited my question to include a working solution, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.