Reputation: 17
I'm getting one element from the ArrayList using the Random Class. Currently I'm trying to cast this element into a string (unsuccessfully) using ToString. How would i Cast Solutions[r] into a string?
ArrayList Solutions = new ArrayList(5);
Solutions.Add("The Odyssey");
Solutions.Add("Dune");
Solutions.Add("Sherlock Holmes");
Solutions.Add("Othello");
Solutions.Add("Of Mice and Men");
Random ran = new Random();
int r = ran.Next(Solutions.Count);
string s = Solutions[r].ToString;
Upvotes: 0
Views: 76
Reputation: 340
List<String> solutions = new List<String>();
solutions.Add("The Odyssey");
solutions.Add("Dune");
// and so on
Then you can:
label1 = solution[random_index]; // starts with 0
Upvotes: 0
Reputation: 8852
using IList<string>
makes it much more easy and clean.
IList<string> Solutions = new List<string>(5);
Solutions.Add("The Odyssey");
Solutions.Add("Dune");
Solutions.Add("Sherlock Holmes");
Solutions.Add("Othello");
Solutions.Add("Of Mice and Men");
Random ran = new Random();
int r = ran.Next(Solutions.Count);
string s = Solutions[r];
Upvotes: 0