Home »
VB.Net »
VB.Net Programs
VB.Net program to demonstrate the MemoryStream class
By Nidhi Last Updated : November 11, 2024
MemoryStream Class in VB.Net
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 code to demonstrate the example of MemoryStream class
'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.
VB.Net StringReader, StringWriter, Stream Programs »