Click here to Skip to main content
16,014,748 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 4-Jul-11 20:50pm
v2
Comments
suryaswathi 5-Jul-11 2:28am    
string[] s = MyValue.Split(Environment.NewLine.ToCharArray() );

i changed this line like dis and declared random function by public

if i do like this mean if i want to generate 10 records mean some times it doesnt generate anyrecord it generate oly less than 10.some of them are blank..why like this its generate

Why would you split on a comma ? There's no commas in your textbox. Split on Environment.NewLine.
 
Share this answer
 
Comments
suryaswathi 5-Jul-11 2:29am    
string[] s = MyValue.Split(Environment.NewLine.ToCharArray() );

i changed this line like dis and declared random function by public

if i do like this mean if i want to generate 10 records mean some times it doesnt generate anyrecord it generate oly less than 10.some of them are blank..why like this its generate.
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:

C#
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
 
Share this answer
 
v3
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
}
 
Share this answer
 
v3
Comments
RaviRanjanKr 10-Jul-11 0:55am    
Always wrap your code in "pre" tags..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900