VB.Net program to demonstrate StringReader class

Here, we are going to demonstrate StringReader class in VB.Net.
Submitted by Nidhi, on January 06, 2021 [Last updated : March 08, 2023]

StringReader Class Example in VB.Net

Here, we demonstrate the StringReader class, we will read the data line by line using the ReadLine() method of StringReader class.

Program/Source Code:

The source code to demonstrate the StringReader class is given below. The given program is compiled and executed successfully.

VB.Net code to demonstrate the example of StringReader class

'Vb.Net program to demonstrate StringReader class.

Imports System.IO

Module Module1
    Class Sample
        Shared str As String = "This is a cat.
This is dog.
This is elephant."

        Shared Sub ReadLines()
            Dim line As String
            Dim line_num As Integer = 0
            Dim flag As Boolean = True
            Dim strReader As New StringReader(str)

            While (flag = True)
                line = strReader.ReadLine()
                If line = Nothing Then
                    flag = False
                Else
                    line_num = line_num + 1
                    Console.WriteLine("Line {0} : {1}", line_num, line)
                End If
            End While
        End Sub
    End Class

    Sub Main()
        Sample.ReadLines()
    End Sub
End Module

Output

Line 1 : This is a cat.
Line 2 : This is dog.
Line 3 : This is elephant.
Press any key to continue . . .

Explanation

In the above program, we created a class module Module1 that contains a class Sample and a Main() function. The Sample class contains a data member str that contains multiple lines of data. It also contains a shared method ReadLine().

In the ReadLines() method, we read the data line by line from data member str using the ReadLine() method of StringReader class and print on the console screen.

The Main() method is the entry point of the program, here we called the shared method ReadLines().

VB.Net StringReader, StringWriter, Stream Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.