Object serialization: Difference between revisions

Content added Content deleted
m (→‎{{header|Wren}}: Minor tidy)
(Added FreeBASIC)
 
Line 889: Line 889:
}
}
</pre>
</pre>

=={{header|FreeBASIC}}==
FreeBASIC does not support object-oriented programming such as classes and inheritance directly. However, we can simulate some aspects of object-oriented programming using procedures and user-defined types (UDTs).

For serialization, FreeBASIC does not have built-in support for this. We need to manually write and read each field of your UDTs to and from a file.
<syntaxhighlight lang="vbnet">Type Person
nombre As String
edad As Integer
End Type

Sub PrintPerson(p As Person)
Print "Name: "; p.nombre
Print "Age: "; p.edad
End Sub

Sub Serialize(p As Person, filename As String)
Open filename For Binary As #1
Put #1, , p.nombre
Put #1, , p.edad
Close #1
End Sub

Sub Deserialize(p As Person, filename As String)
Open filename For Binary As #1
Get #1, , p.nombre
Get #1, , p.edad
Close #1
End Sub

Dim pp As Person
pp.nombre = "Juan Hdez."
pp.edad = 52

Serialize(pp, "objects.dat")
Deserialize(pp, "objects.dat")

PrintPerson(pp)

Sleep</syntaxhighlight>
{{out}}
<pre>Name: Juan Hdez.
Age: 52</pre>


=={{header|Go}}==
=={{header|Go}}==