Reputation: 5375
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)];
}
Upvotes: 0
Views: 3858
Reputation: 5375
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);
}
Upvotes: 0
Reputation: 10184
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)];
}
}
Upvotes: 2
Reputation: 411
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);
Upvotes: 1
Reputation: 2830
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);
}
Upvotes: -2