dimanche 19 février 2017

Creating specified number of strings VB.NET

I want to create specified number of strings in a Console VB.NET, I've tried these code and it thrown an exception: NullReferenceException: Object Reference not set into an instance of an Object.

These are the code:

Module RandStrConsole

Dim r As New Random
Dim s As String
Dim result As System.Text.StringBuilder
Dim sb As System.Text.StringBuilder
Dim MaxChar As Integer


Sub Main()

    Console.Write("Enter String: ") : s = Console.ReadLine
    Console.Write("Maxchar: ") : MaxChar = Console.ReadLine

    For i As Integer = 1 To MaxChar

        Dim idx As Integer = r.Next(0, s.Count - 1)
        result = sb.Append(s.Substring(idx, 1)) 'NullReferenceException: Object Reference not set into an instance of an Object.
        result.ToString()

    Next

    Console.WriteLine(result)
    Console.ReadKey()


End Sub

End Module

The comment section indicates where the exception happened. Pls I'm newbie here at VB .NET and I want to learn more about VB .NET Thanks.




Aucun commentaire:

Enregistrer un commentaire