0

as in my previous question Randomize Lines order in textbox with StringBuilder nobody really answer it with good real answer so i randomize the lines with another way

i want to put to array the final result then shuffle it , then turn it to string again.

   Dim sb As New StringBuilder
       For Each line In texbox1.text.Lines
       sb.AppendLine(line & " CODE-DONE-1")
       sb.AppendLine(line & " CODE-DONE-2")
       sb.AppendLine(line & " CODE-DONE-3")

    Next
textbox2.text = sb.ToString 'Want to randomize Lines Order

my attempt

    Dim sb As New StringBuilder
    For Each line In texbox1.text.Lines
        sb.AppendLine(line & " CODE-DONE-1")
        sb.AppendLine(line & " CODE-DONE-2")
        sb.AppendLine(line & " CODE-DONE-3")

    Next
    Dim list = New List(Of String) From {sb.ToString}
    Dim rnd As New Random()
    Dim shuffled = list.OrderBy(Function(x) rnd.Next()).ToList()
    TextBox2.Text = shuffled

getting alot of errors , i hope you get idea what i want to do , i want to Shuffle output Lines in sb.ToString

4
  • How about not using a StringBuilder before shuffling, and only using it after shuffling to create your final string? Seems your variable list above only has one element - the entire string with all lines. First create an Array or List of your lines, then call .OrderBy( as you have done. Then second, loop through your randomized list and build your final string. Commented Apr 21, 2020 at 19:29
  • 3
    I didn't have a problem with JQSOFT's answer in the other question, and s/he's a smart person; what you were told there is right and will work Commented Apr 21, 2020 at 19:31
  • @CaiusJard not what i need at all , his answer Shuffle data coming from textbox1 , then i do my code then export to textbox2, what i need is shuffle the final data after my coding right before exporting to textbox2 , his answer kinda useless because it will export same data Commented Apr 21, 2020 at 19:33
  • 1
    What? @JQSoft's answer should work for you. Have you tried the code? What was the result? Commented Apr 22, 2020 at 4:39

2 Answers 2

1

Recapping the answer by @Calus Jard and JQSOFT.

1.Declare a variable at form level for the Random class

2.Declare a List(Of T)

3.Add your lines to the list.

4.Shuffle the list with the OrderBy and display in TextBox2

Private rand As New Random
Private Sub OPCode()
    Dim lst As New List(Of String)
    For Each line In TextBox1.Lines
        lst.Add(line & " CODE-DONE-1")
        lst.Add(line & " CODE-DONE-2")
        lst.Add(line & " CODE-DONE-3")
    Next
    TextBox2.Lines = lst.OrderBy(Function(x) rand.Next).ToArray
End Sub

Credit goes to JQSoft and Calus. This is just a recap.

Sign up to request clarification or add additional context in comments.

2 Comments

works good , if i want to save it with IO.File.WriteAllText(SaveFileDialog1.FileName, TextBox2.Lines ) String() cannot be converted to String
No. The second parameter of WriteAllText is a string. Lines is an array. Your need to start looking things up. learn.microsoft.com/en-us/dotnet/api/… It should be IO.File.WriteAllText(SaveFileDialog1.FileName, TextBox2.Text )
1

You're creating a problem here by using a stringbuilder. They are for building a single string. You can AppendLine if you want, but all that will do is make the single one existing string in the builder into a LONGER string with a CRLF character pair on the end. There isn't this concept of "lines" in a stringbuilder in the sense of "a collection of individual strings that you can shuffle around" like there is with e.g. a TextBox's .Lines property (which really is an array of strings, but there is a conversion step that goes between the single-string-with-CRLF-characters-in-it and array-of-strings-one-string-per-line)

Basically a textbox splits the text on CRLF when you ask for its Lines and if you set its lines it joins the array you give it together with CRLF to make one long string

I recommend you throw away the stringbuilder and just shuffle the lines in the textbox:

Dim rnd as New Random
Dim shuffledX = textbox1.Lines.OrderBy(Function(x) rnd.Next()).ToArray()

All this "getting lines out of a textbox, putting them into a builder, tostringing the builder (makes a single string), putting it in a List (your list has ONE string in it), getting the strings (there is only one) out of the list in random order....

Just throw it all away. Shuffle the .Lines


If you read a file to get your lines, you have a lines array, you can shuffle it!:

Dim x = File.ReadAllLines("path...")

Dim shuffledX = x.OrderBy(...).ToArray()

If your user gives you lines one at a time, put them into a List(Of String) and shuffle it:

Dim x as New List(Of String)
x.Add(Console.ReadLine())
x.Add(Console.ReadLine())
x.Add(Console.ReadLine())
x.Add(Console.ReadLine())
x.Add(Console.ReadLine())

Dim shuffledX = x.OrderBy(...).ToArray()

If you really are getting a string or a stringbuilder from somewhere, then split it on CRLF:

Dim s = $"My{Environment.NewLine}string{Environment.NewLine}with{Environment.NewLine}lines"
Dim x = s.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)

Dim shuffledX = a.OrderBy(...).ToArray()

Once you have your shuffled lines

If you want to put them to a textbox, and theyre an array, you can assign them to the textbox Lines property:

textbox2.Lines = shuffledX

If you want to write them to a file, write the array:

File.WriteAllLines(...)

If you want to pump them to the console:

For Each s in shuffledX
  Console.WriteLine(s)
Next s

If you need them as a string, a single string, then join the array into a single string:

Dim s = string.Join(Environment.NewLine, shuffledX)

You can set a textbox.Text = s, or put it in a stringbuilder (For Each t in s sb.AppendLine(t), or write a file (File.WriteAllText("path", s)), dump it to console (Console.Write(s))...


In all this, you have to be clear about what a stringbuilder is and isn't, what a string is and isn't and what an array of strings is/isn't. You have to use the most appropriate data storage for the task; if an array is coming in, shuffle the array. If a single string is coming in, split it to an array and shuffle the array. Don't take an array and put it into a stringbuilder and ask us to help shuffling the stringbuilder - it's madness, the only thing you can do there is undo the wrong step (of putting into the stringbuilder) by stringBuilder.ToString().Split() to undo the wrong step of putting the array into the stringbuilder in the first place - turning the stringbuilder into a string and then splitting the string gets back to the array that can be shuffled.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.