[英]Cast From Arraylist To a String
I'm getting one element from the ArrayList using the Random Class. 我使用Random Class从ArrayList中获取一个元素。 Currently I'm trying to cast this element into a string (unsuccessfully) using ToString.
目前我正在尝试使用ToString将此元素转换为字符串(失败)。 How would i Cast Solutions[r] into a string?
我如何将解决方案[r]转换为字符串?
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;
string s = Solutions[r].ToString(); // you're missing brackets
using IList<string>
makes it much more easy and clean. 使用
IList<string>
使它更容易和干净。
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];
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.