1
Answer

C# Shuffle string in list & display the output to a textbox

Ask a question
 

private void Shufflebutton_Click(object sender, EventArgs e)

{

List<int> myList = new List<int>();

int[] order;

Random random = new Random();

StringBuilder sb = new StringBuilder();

foreach (String suit in new string[] { "a", "b" })

{

foreach (string value in new String[] { "2", "3", "4", "5" } )

{

sb.Append(suit);

sb.Append(value);

sb.Append(" ");

}

sb.Append(Environment.NewLine);

}

myList = sb.ToList(); // error at this line

order = new int[myList.Count];

for (int i = 0; i <myList.Count; i++)

{

order[i] = i;

}

for (int i = 0; i < order.Length - 1; i++)

{

        int rnd = random.Next(i + 1, order.Length);

       int temp = order[i];

order[i] = order[rnd];

order[rnd] = temp;

 } 

           ShuffletextBox.Text = order[rnd].ToString();  //iam getting error at this line

}




I am trying to save the string in the List and then wants to shuffle the list and display the output in a textbox. Can some one help me?? Recently started learning c#. Any help greatly appreciated.




Answers (1)