Reputation: 9
I am trying to make a program where the user enters a string and the string is then stored in a List named word1. I want to pick a random string from the List, and display it in a label. I'm trying to do this using multiple methods and classes as practice. Here's my code:
This is in the class Class1.cs:
main main = new main();
public string flashCards1()
{
List<string> word1 = main.GetList1();
Random rnd = new Random();
int rtn = rnd.Next(word1.Count - 1);
string word = word1[rtn];
string rtnWord = word.ToString();
return rtnWord;
}
This one is in main.cs (not Main) and it talks to Class1. Like I said this part is probably unnecessary but I was trying to practice with multiple methods.
private List<string> word2 = new List<string>();
private List<string> word1 = new List<string>();
public List<string> GetList1()
{
return word1;
}
public void SetList1(List<string> updatedList)
{
word1 = updatedList;
}
This one is in Form1.cs and sets the label to the return value of flashCards1:
private void go_Click(object sender, EventArgs e)
{
menu.Visible = false;
cards.Visible = true;
label1.Text = class1.flashCards1();
}
This is also in Form1.cs and saves the text to the List:
private void enter_Click(object sender, EventArgs e)
{
List<string> word1 = main.GetList1();
word1.Add(textBox1.Text);
main.SetList1(word1);
}
When I run this code it gives my the error:
System.ArgumentOutOfRangeException was unhandled HResult=-2146233086 Message='maxValue' must be greater than zero. Parameter name: maxValue ParamName=maxValue Source=mscorlib StackTrace: at System.Random.Next(Int32 maxValue) at WindowsFormsApplication1.Class1.flashCards1() in C:\Users\lottes\Source\Workspaces\Workspace\WindowsFormsApplication1\WindowsFormsApplication1\Class1.cs:line 19 at WindowsFormsApplication1.Form1.go_Click(Object sender, EventArgs e) in C:\Users\lottes\Source\Workspaces\Workspace\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs:line 62 at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run(Form mainForm) at WindowsFormsApplication1.Program.Main() in C:\Users\lottes\Source\Workspaces\Workspace\WindowsFormsApplication1\WindowsFormsApplication1\Program.cs:line 19 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException:
I've tried to enter lots of values but it doesn't seem to work. I also looked at this thread: How could I get a random string from a list and assign it to a variable
However their solutions gave me the same error. Any help would be greatly appreciated. Sorry if this was too much/little information. I tried to make sense.
Upvotes: 0
Views: 329
Reputation: 837
Your problem is with the Random.Next(int) method. This exception comes up when you try use a negative value as your maximum parameter. This is because it returns a value between >= 0 and < maxValue
. Which gets messed up by negative values (-1 in your case when the list has 0 items)
The fix is easy, just change :
int rtn = rnd.Next(word1.Count - 1);
into:
int rtn = rnd.Next(word1.Count);
This should work because random always returns a value less than the maximum value, so should access 0 through N - 1, where N is the size of your list.
You're going to have further problems after that one (and less related to this direct question) though because the core issue is that you haven't initialized your list and added values before using it in the flashcards1()
function. It's not clear how to solve that in the examples you've shown us, but I'd suggest looking at the order in which your functions execute.
Upvotes: 5