Konrad Czajkowski Konrad Czajkowski - 9 months ago 32
Vb.net Question

For loop not outputting properly when using arrays

I would expect this code when run to output the first line of the array, pause, then output the second line in the array, each line being output with a delay between each character but for some reason it isn't working for me.

Dim IntroText(4) As String
IntroText(0) = "Konrad Czajkowski..."
IntroText(1) = "...Presents"
IntroText(2) = "...A text based game..."
IntroText(3) = "...The Legend of Konrad and The Quest for Skairum"

Dim IntroTextLength As Integer = Nothing
IntroTextLength = IntroText(IntroText.Length - 1)

For IntroCounter1 = 0 To IntroTextLength
For IntroCounter2 = 0 To IntroText(IntroCounter1).Length - 1
Console.Write(IntroText(IntroTextLength)(IntroCounter2))
Threading.Thread.Sleep(50)
Next
Threading.Thread.Sleep(5000)
Console.Clear()
Next
Console.Clear()


P.s I'm using a console application in VB.NET

Answer Source

Try this.

Dim IntroText(4) As String
    IntroText(0) = "Konrad Czajkowski..."
    IntroText(1) = "...Presents"
    IntroText(2) = "...A text based game..."
    IntroText(3) = "...The Legend of Konrad and The Quest for Skairum"

    Dim IntroTextLength As Integer = Nothing
    IntroTextLength = IntroText.Length - 1

    For IntroCounter1 = 0 To IntroTextLength - 1
        For IntroCounter2 = 0 To IntroText(IntroCounter1).Length - 1
            Console.Write(IntroText(IntroCounter1)(IntroCounter2))
            'Console.Write(IntroTextLength)
            Threading.Thread.Sleep(50)
        Next
        Threading.Thread.Sleep(500)
        Console.Clear()
    Next
    Console.Clear()