Skip to content

Commit

Permalink
Implement serialization and deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
aiden2480 committed Nov 19, 2021
1 parent 91bc610 commit 1f07084
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion VB-Pizza-v104/Pizza104.vb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Imports System.Text.RegularExpressions
Imports System.Text.RegularExpressions
Imports System.Xml.Serialization

Public Class Pizza104
' Set up a class for each order
Expand All @@ -15,6 +16,11 @@ Public Class Pizza104
Public deliveryDate As Date
Public deliveryTime As String

' Required for serialization
Public Sub New()
End Sub

' Actial inisialisation function
Public Sub New(firstName As String, lastName As String, phoneNo As String, address As String,
postcode As String, quantity As Byte, crustType As Char, toppings As List(Of String),
deliveryDate As Date, deliveryTime As String)
Expand All @@ -34,6 +40,8 @@ Public Class Pizza104

' Establish variables
Shared ReadOnly orders As New List(Of PizzaOrder)
ReadOnly cereal As New XmlSerializer(GetType(List(Of PizzaOrder)))

Dim crustCost As Double
Dim toppingsCost As Double
Dim resetting As Boolean = False
Expand Down Expand Up @@ -82,6 +90,9 @@ Public Class Pizza104

' Render all orders in the form box
DisplayList()

' Test serialization and deserialization functions
Debug.WriteLine("Field 0 address: " + DeserializeOrders(SerializeOrders())(0).address)
End Sub

Private Sub AddOrderButtonClicked(sender As Object, e As EventArgs) Handles btnAddOrder.Click
Expand Down Expand Up @@ -258,6 +269,26 @@ Public Class Pizza104
resetting = False
End Sub

' Functions for serializing and deserializing the objects for long term storage on the hard disk
' Algorithms attributed to http://www.vb-helper.com/howto_net_serialize.html
Private Function SerializeOrders()
Dim writer As New IO.StringWriter()

cereal.Serialize(writer, orders)
writer.Close()

Return writer.ToString()
End Function

Private Function DeserializeOrders(soup As String)
Dim reader As New IO.StringReader(soup)

Dim obj As List(Of PizzaOrder) = DirectCast(cereal.Deserialize(reader), List(Of PizzaOrder))
reader.Close()

Return obj
End Function

' These functions can be used in two different contexts:
' 1. Update cost text fields as the user changes checkboxes (from event changes)
' 2. Concatenate text fields as required and return their values for processing
Expand Down

0 comments on commit 1f07084

Please sign in to comment.