user2390516
user2390516

Reputation: 17

Cast From Arraylist To a String

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

Answers (3)

Half_Baked
Half_Baked

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

Mayank
Mayank

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

Konstantin
Konstantin

Reputation: 3294

string s = Solutions[r].ToString(); // you're missing brackets

Upvotes: 2

Related Questions