文本框值使用arraylist一对一 [英] textbox values getting one by one using arraylist

查看:59
本文介绍了文本框值使用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)(范围从0lines.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 use ArrayList, it is obsolete and inconvenient due to the need of element type cast, which is error-prone, too. Instead, use generic equivalent System.Collections.Generic.List. The type ArrayList 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 of lines.Length and use it in a randomly generated index in this array using the class System.Random. Do not create an instance of the class Random in loop (a common mistake) — make it only once in the lifetime of your application. Use the method System.Random.Next(Int32, Int32) (with range 0 to lines.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆