0

fairly new C# developer here. I'm trying attempting to develop a mad lib generator. I have a button button_1 labeled "verb" that is supposed to generate a random verb. The verb comes from a string array, which is a list of verbs. I have another button button_5 labeled "add new verb" that is supposed to add the verb in the corresponding text box to the verb array. The problem I'm having is that it is only generating the last verb that I entered when I click button_1 which is labeled "verb".

Here is what the code looks like:

namespace WindowsFormsApplication1
{
    public class Arrays
    {
        public static string[] verbarray = new string[10];
    }
}

public void button5_Click(object sender, EventArgs e)
{
    for (int iverb = 0; iverb < Arrays.verbarray.Length; iverb++)
    {
        Arrays.verbarray[iverb] = Convert.ToString(this.txtaddverb.Text);
    }
}

public void button1_Click(object sender, EventArgs e)
{
    Random randomverb = new Random();
    verb.Text = Arrays.verbarray[randomverb.Next(0, Arrays.verbarray.Length)];
}
2
  • Your button5_Click() procedure is repopulating every element in your array with the current value of txtAddVerb.Text...so, when you click button1, you get a random selection from a list of identical array entries. I'll move this to an answer and add more detail. Commented Feb 27, 2013 at 16:50
  • I have edited your title. Please see, "Should questions include “tags” in their titles?", where the consensus is "no, they should not". Commented Feb 27, 2013 at 18:00

5 Answers 5

2

Your verb-adding procedure is the problem.

You have set up a loop that iterates across all entries in your verb array, and replaces each one with the current value of your verb textbox. So, when you click the button to select a new verb, you're randomly selecting a verb from a list of entries that's always going to be identical once you've started adding verbs - and the verb chosen will always be the last one you added.

I'd suggest you take a look at a List, which can grow more readily and simplifies your Add problem. Might help!

// a little pseudocode to help with the notion..wire in your 
// event handlers accordingly
class VerbManager
{

 List<String> verbs= new List<String>();
 Random picker = new Random();

 public void addVerb(String newVerb)
 {
     verbs.Add(newVerb);
 }

 public string pickRandomVerb()
 {
     return verbs[picker.Next(0,verbs.Count)];
 }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Great! Glad it was helpful. Might consider picking it as the answer if you're so inclined. Have an awesome day!
1

In C# Arrays are of a fixed length, therefore, if your application is based around being able to add verbs to the common pool of verbs dynamically you should consider changing

public static string[] verbarray = new string[10];

to

public static List<string> verbList = new List<string>();

A List can grow in size, so if you already had 10 verbs and wanted to add another then that would be no problem. The code required in button5_Click would then simply read:

verbList.Add(txtaddverb.Text);

Comments

0

Move:

Random randomverb = new Random(); 

to the class level.

Comments

0

Here is the code I ended up using after solving all bug problems:

namespace WindowsFormsApplication1

{

public class Lists

{
    public static List<string> verbList = new List<string>();

    public static Random randomverb = new Random();
}

}

public string pickRandomVerb()

    {
        return Lists.verbList[Lists.randomverb.Next(0, Lists.verbList.Count)];
    }
    public void button1_Click(object sender, EventArgs e)
    {
        if (Lists.verbList.Count > 0) verb.Text = pickRandomVerb();
    }

public void button5_Click(object sender, EventArgs e)

    {
        Lists.verbList.Add(txtaddverb.Text);
    }

Comments

-2

You should correct your logic here:

   public void button5_Click(object sender7, EventArgs e)
    {
        for (int iverb = 0; iverb < Arrays.verbarray.Length; iverb++)
        {
            Arrays.verbarray[iverb] = Convert.ToString(this.txtaddverb.Text);
        }
    }

to what you want

public void button5_Click(object sender7, EventArgs e)
        {
            // suppose index 6
                Arrays.verbarray[6] = Convert.ToString(this.txtaddverb.Text);

        }

3 Comments

Why would you suggest he hardcode an array element value like that??
I just give him idea where he is wrong, he could take an integer in form and can do what he want in place of 6
To make your answer more viable you should show what you did where the fault is, then explain why it is wrong. Then provide a valid solution. It will make your answer more beneficial. Thank you for the contribution though.

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.