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