lang • special • read


Public Sub _read ( Stream As Stream )

The _read method is called by READ instruction to unserialize an object.

The READ instruction will first instanciate the object, and then call the _read method on it.

This will work only if the _new constructor can be called with no argument.

The method takes the target stream as argument, and must read the stream to initialize the newly created object.

This method returns nothing.


Dim Item1 As New MyClass = 123
Item1.def = "456"
Print "In:  " & & "-" & Item1.def

Dim f1 As File
f1 = Open "~/item.bin" For Write Create
Write #f1, Item1 As Object 'MyClass
Close f1

Shell "hexdump -C ~/item.bin"

Dim f2 As File
f2 = Open "~/item.bin" For Read
Dim Item2 As MyClass = Read #f2 As MyClass
Close f2

Print "Out: " & & "-" & Item2.def

Class MyClass
  Public abc As Integer
  Public def As String
  Public Sub _read(As Stream)
    abc = Read #As Integer
    def = Read #As String
  Public Sub _write(As Stream)
    Write #f, abc As Integer
    Write #f, def As String
End Class
00000000  6f 07 4d 79 43 6c 61 73  73 7b 00 00 00 03 34 35  |o.MyClass{....45|
00000010  36                                                |6|
In:  123-456
Out: 123-456

See also