文本框值使用arraylist一对一 [英] textbox values getting one by one using arraylist
问题描述
嗨
在弹出窗口中,我有一个多行文本框.在其中键入五个名称,例如
mahi
moni
索尼
suji
krish
如果我给出确定的意思是它想使用数组列表存储临时变量
并希望使用随机函数选择任何一个名称..
我尝试过此编码
{ a2.Add(item); rno1 = randmname.Next(a2.Count); rno2 = a2[rno1].ToString(); } private static int rno, rno1; private static string rno2; private string stringToArrayList(string MyValue) { ArrayList a2 = new ArrayList(); string[] s = MyValue.Split(new char[] { '','' }); foreach (string item in s) a2.Add(item); Random randmname = new Random(); rno1 = randmname.Next(a2.Count); rno2 = a2[rno1].ToString(); return rno2; }
我正在此函数中传递我的textbox.textcontent.
这里的所有名称都像单行中的mahimonisonisujikrish
但我想从此列表中生成随机名称
我犯了什么错误.我该如何更改
为什么要用逗号分割?您的文本框中没有逗号.首先,不要使用ArrayList
,由于元素类型转换的需要,它既过时又不方便,这也容易出错.相反,请使用等效的通用System.Collections.Generic.List
.在v.2.0中引入泛型后,类型ArrayList
变得过时了.
您需要以其他方式拆分:
字符串 []行= myTextBox.Text.Split( 新 字符串 [] {System.Environment.NewLine});
现在,获取lines.Length
的行号,并使用类System.Random
在此数组中的随机生成的索引中使用它.不要循环创建类Random
的实例(这是一个常见错误),请在应用程序的生命周期内仅创建一次.使用方法System.Random.Next(Int32, Int32)
(范围从0
到lines.Length-1
).
—SA
我同意
-SA
在Arraylist的位置使用通用集合,例如:
class Info { public string Name{get;set;} } List oListInfo = new List(); private string stringToArrayList(string MyValue) { string[] s = MyValue.Split(new string[] { Environment.NewLine }); foreach (string item in s) oListInfo.Name = item ; //Write other logic }
Hi
In a popup i have a multiline textbox .in that i type five names like
mahi
moni
soni
suji
krish
if i give ok mean it want to store temporary variable using array list
and want to select any one name using random function..
i tried this coding
{ a2.Add(item); rno1 = randmname.Next(a2.Count); rno2 = a2[rno1].ToString(); } private static int rno, rno1; private static string rno2; private string stringToArrayList(string MyValue) { ArrayList a2 = new ArrayList(); string[] s = MyValue.Split(new char[] { '','' }); foreach (string item in s) a2.Add(item); Random randmname = new Random(); rno1 = randmname.Next(a2.Count); rno2 = a2[rno1].ToString(); return rno2; }
i am passing my textbox.textcontent in this function.
here am getting like all names mahimonisonisujikrish in single line
but i want to generate random names from this list
what mistake i done.how can i change
Why would you split on a comma ? There''s no commas in your textbox. Split on Environment.NewLine.
First of all, never useArrayList
, it is obsolete and inconvenient due to the need of element type cast, which is error-prone, too. Instead, use generic equivalentSystem.Collections.Generic.List
. The typeArrayList
was rendered obsolete with introduction of generics in v.2.0.
You need to split in a different way:
string[] lines = myTextBox.Text.Split( new string[] { System.Environment.NewLine });
Now, get line number oflines.Length
and use it in a randomly generated index in this array using the classSystem.Random
. Do not create an instance of the classRandom
in loop (a common mistake) — make it only once in the lifetime of your application. Use the methodSystem.Random.Next(Int32, Int32)
(with range0
tolines.Length-1
).
—SA
I am agree with
-SA
On the place of Arraylist use generic collection for example:
class Info { public string Name{get;set;} } List oListInfo = new List(); private string stringToArrayList(string MyValue) { string[] s = MyValue.Split(new string[] { Environment.NewLine }); foreach (string item in s) oListInfo.Name = item ; //Write other logic }
这篇关于文本框值使用arraylist一对一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!