VB.Net program to demonstrate the MemoryStream class

Here, we are going to demonstrate the MemoryStream class in VB.Net.
Submitted by Nidhi, on January 06, 2021

Here, we will demonstrate MemoryStream class and write data into the memory stream using Write() and WriteByte() method.

Program/Source Code:

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

'Vb.Net program to demonstrate MemoryStream class.

Imports System.IO

Module Module1
    Sub Main()
        Dim ms As MemoryStream
        Dim count As Integer = 0

        Dim byteBuff1() As Byte = {97, 98, 99}
        Dim byteBuff2() As Byte = {65, 66, 67}

        ms = New MemoryStream(50)

        ms.Write(byteBuff1, 0, 3)

        While (count < byteBuff2.Length)
            ms.WriteByte(byteBuff2(count))
            count = count + 1
        End While

        ms.Seek(0, SeekOrigin.Begin)

        Dim byteArray(ms.Length) As Byte

        count = ms.Read(byteArray, 0, byteArray.Length)

        ms.Close()

        Console.WriteLine("Data written into memory stream:")
        For Each b In byteArray

            Console.Write(Convert.ToChar(b))
        Next
        Console.WriteLine()
    End Sub
End Module

Output:

Data written into memory stream:
abcABC
Press any key to continue . . .

Explanation:

In the above program, we created a class module Module1 that a Main() function. The Main() method is the entry point of the program, here we created an object of MemoryStream class and write bytes using Write() and WriteByte() method then reset the read pointer to the beginning of the memory stream and then read byte array from the memory stream using Read() method and print character corresponding to each byte value by character typecasting on the console screen.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.