Home

MS Visual Basic .NET Applications:
Georgetown Cleaning Services

 

Introduction to Serialization

 

Cleaning Order Processing

To process an order, we will use the same approach as for the Georgetown Cleaning Services application to calculation the cost of a cleaning order

When an order has been processed, we will let the user save it. To make this useful and convenient, we will save cleaning orders for each day in a common file but we will give the user the opportunity to save the order for another day. To implement this behavior, we will use the serialization of an ArrayList object which itself will use a whole cleaning order stored in a variable.

Practical Learning Practical Learning: Creating a Serializable Class

  1. In the Solution Explorer, right-click the References node and click Add Reference...
  2. In the .NET property page of the Add Reference dialog box, select System.Runtime.Serialization.Formatters.Soap and click Select
     
    Add Reference
  3. Click OK
  4. To add a new class, on the main menu, click Project -> Add Class...
  5. Set the Name to CleaningOrder and click Open
  6. Change the file as follows:
     
    <Serializable()> Public Class CleaningOrder
        Public OrderID As Integer
        Public OrderNumber As String
        Public CustomerName As String
        Public CustomerPhone As String
        Public DateLeft As DateTime
        Public TimeLeft As DateTime
        Public DatePickedUp As DateTime
        Public TimePickedUp As DateTime
        Public UnitPriceShirts As Decimal
        Public QuantityShirts As Integer
        Public SubTotalShirts As Double
        Public UnitPricePants As Double
        Public QuantityPants As Integer
        Public SubTotalPants As Double
        Public Item1Name As String
        Public UnitPriceItem1 As Double
        Public QuantityItem1 As Integer
        Public SubTotalItem1 As Double
        Public Item2Name As String
        Public UnitPriceItem2 As Double
        Public QuantityItem2 As Integer
        Public SubTotalItem2 As Double
        Public Item3Name As String
        Public UnitPriceItem3 As Double
        Public QuantityItem3 As Integer
        Public SubTotalItem3 As Double
        Public Item4Name As String
        Public UnitPriceItem4 As Double
        Public QuantityItem4 As Integer
        Public SubTotalItem4 As Double
        Public CleaningTotal As Double
        Public TaxRate As Double
        Public TaxAmount As Double
        Public OrderTotal As Double
    
        Public Sub New()
            OrderID = 0
            OrderNumber = "010119000"
            CustomerName = "Unknown"
            CustomerPhone = "N/A"
            DateLeft = DateTime.Today
            TimeLeft = DateTime.Now
            DatePickedUp = DateTime.Today
            TimePickedUp = DateTime.Now
            UnitPriceShirts = 0
            QuantityShirts = 0
            SubTotalShirts = 0
            UnitPricePants = 1.75
            QuantityPants = 0
            SubTotalPants = 0.0
            Item1Name = "None"
            UnitPriceItem1 = 0.0
            QuantityItem1 = 0
            SubTotalItem1 = 0.0
            Item2Name = "None"
            UnitPriceItem2 = 0.0
            QuantityItem2 = 0
            SubTotalItem2 = 0.0
            Item3Name = "None"
            UnitPriceItem3 = 0.0
            QuantityItem3 = 0
            SubTotalItem3 = 0.0
            Item4Name = "None"
            UnitPriceItem4 = 0.0
            QuantityItem4 = 0
            SubTotalItem4 = 0.0
            CleaningTotal = 0.0
            TaxRate = 5.75
            TaxAmount = 0.0
            OrderTotal = 0.0
        End Sub
    End Class
  7. Display the Cleaning Orders form and double-click an unoccupied area of its body
  8. In the top section of the file, type:
     
    Imports System.IO
    Imports System.Runtime.Serialization.Formatters.Soap
    
    Public Class Form1
        Inherits System.Windows.Forms.Form
    
        Dim CleaningOrders As ArrayList
  9. Implement the Load event as follows:
     
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
            CleaningOrders = New ArrayList
    End Sub
  10. In the Class Name combo box, select btnSave
  11. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
            Dim orderID As Integer = 0
            Dim CleaningOrders As ArrayList = New ArrayList
            Dim strFilename As String = "CleaningOrders.gcs"
            Dim gcsSoap As SoapFormatter = New SoapFormatter
    
            If File.Exists(strFilename) Then
                Dim gcsStream As FileStream = New FileStream(strFilename, FileMode.Open, FileAccess.Read, FileShare.Read)
                CleaningOrders = gcsSoap.Deserialize(gcsStream)
    
                Dim order As CleaningOrder = CleaningOrders(CleaningOrders.Count - 1)
                orderID = order.OrderID
                gcsStream.Close()
            Else
                Dim gcsStream As FileStream = New FileStream(strFilename, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write)
                gcsSoap.Serialize(gcsStream, CleaningOrders)
                gcsStream.Close()
            End If
    
            If Me.txtCustomerName.Text = "" Then
                MsgBox("A cleaning order must have a name for the customer")
                Me.txtCustomerName.Focus()
                Exit Sub
            End If
    
            CalculateTotal()
    
            Dim cleanOrder As CleaningOrder = New CleaningOrder
            cleanOrder.OrderID = orderID + 1
            cleanOrder.CustomerName = Me.txtCustomerName.Text
            cleanOrder.CustomerPhone = Me.txtCustomerPhone.Text
            cleanOrder.DateLeft = Me.dtpDateLeft.Value
            cleanOrder.TimeLeft = Me.dtpTimeLeft.Value
            cleanOrder.DatePickedUp = Me.dtpDateExpected.Value
            cleanOrder.TimePickedUp = Me.dtpTimeExpected.Value
            cleanOrder.UnitPriceShirts = CDbl(Me.txtShirtsUnitPrice.Text)
            cleanOrder.QuantityShirts = CInt(Me.txtShirtsQuantity.Text)
            cleanOrder.SubTotalShirts = CDbl(Me.txtShirtsSubTotal.Text)
            cleanOrder.UnitPricePants = CDbl(Me.txtPantsUnitPrice.Text)
            cleanOrder.QuantityPants = CInt(Me.txtPantsQuantity.Text)
            cleanOrder.SubTotalPants = CDbl(Me.txtPantsSubTotal.Text)
            cleanOrder.Item1Name = Me.cboItem1.Text
            cleanOrder.UnitPriceItem1 = CDbl(Me.txtItem1UnitPrice.Text)
            cleanOrder.QuantityItem1 = CInt(Me.txtItem1Quantity.Text)
            cleanOrder.SubTotalItem1 = CDbl(Me.txtItem1SubTotal.Text)
            cleanOrder.Item2Name = Me.cboItem2.Text
            cleanOrder.UnitPriceItem2 = CDbl(Me.txtItem2UnitPrice.Text)
            cleanOrder.QuantityItem2 = CInt(Me.txtItem2Quantity.Text)
            cleanOrder.SubTotalItem2 = CDbl(Me.txtItem2SubTotal.Text)
            cleanOrder.Item3Name = Me.cboItem3.Text
            cleanOrder.UnitPriceItem3 = CDbl(Me.txtItem3UnitPrice.Text)
            cleanOrder.QuantityItem3 = CInt(Me.txtItem3Quantity.Text)
            cleanOrder.SubTotalItem3 = CDbl(Me.txtItem3SubTotal.Text)
            cleanOrder.Item4Name = Me.cboItem4.Text
            cleanOrder.UnitPriceItem4 = CDbl(Me.txtItem4UnitPrice.Text)
            cleanOrder.QuantityItem4 = CInt(Me.txtItem4Quantity.Text)
            cleanOrder.SubTotalItem4 = CDbl(Me.txtItem4SubTotal.Text)
            cleanOrder.CleaningTotal = CDbl(Me.txtCleaningTotal.Text)
            cleanOrder.TaxRate = CDbl(Me.txtTaxRate.Text)
            cleanOrder.TaxAmount = CDbl(Me.txtTaxAmount.Text)
            cleanOrder.OrderTotal = CDbl(Me.txtNetPrice.Text)
            Me.CleaningOrders.Add(cleanOrder)
    
            Dim stmOrders As FileStream = New FileStream(strFilename, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write)
            gcsSoap.Serialize(stmOrders, Me.CleaningOrders)
            stmOrders.Close()
    
            Me.btnReset_Click(sender, e)
    End Sub
  12. Execute the application
  13. Process a few orders and save each
     
  14. Close the form and return to your programming environment
  15. In the Class Name combo box, select btnOpen
  16. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub btnOpen_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOpen.Click
            Dim lstCleaningOrders As ArrayList = New ArrayList
            Dim strFilename As String = "CleaningOrders.gcs"
            Dim gcsSoap As SoapFormatter = New SoapFormatter
            Dim receiptNumber As Integer = CInt(Me.txtReceiptNumber.Text)
    
            If File.Exists(strFilename) Then
                Dim gcsStream As FileStream = New FileStream(strFilename, FileMode.Open, FileAccess.Read, FileShare.Read)
                lstCleaningOrders = gcsSoap.Deserialize(gcsStream)
                gcsStream.Close()
    
                For Each order As CleaningOrder In lstCleaningOrders
                    If order.OrderID = receiptNumber Then
                        Me.txtCustomerName.Text = order.CustomerName
                        Me.txtCustomerPhone.Text = order.CustomerPhone
                        Me.dtpDateLeft.Value = order.DateLeft
                        Me.dtpTimeLeft.Value = order.TimeLeft
                        Me.dtpDateExpected.Value = order.DatePickedUp
                        Me.dtpTimeExpected.Value = order.TimePickedUp
                        Me.txtShirtsUnitPrice.Text = order.UnitPriceShirts.ToString()
                        Me.txtShirtsQuantity.Text = order.QuantityShirts.ToString()
                        Me.txtShirtsSubTotal.Text = order.SubTotalShirts.ToString()
                        Me.txtPantsUnitPrice.Text = order.UnitPricePants.ToString()
                        Me.txtPantsQuantity.Text = order.QuantityPants.ToString()
                        Me.txtPantsSubTotal.Text = order.SubTotalPants.ToString()
                        Me.cboItem1.Text = order.Item1Name
                        Me.txtItem1UnitPrice.Text = order.UnitPriceItem1.ToString()
                        Me.txtItem1Quantity.Text = order.QuantityItem1.ToString()
                        Me.txtItem1SubTotal.Text = order.SubTotalItem1.ToString()
                        Me.cboItem2.Text = order.Item2Name
                        Me.txtItem2UnitPrice.Text = order.UnitPriceItem2.ToString()
                        Me.txtItem2Quantity.Text = order.QuantityItem2.ToString()
                        Me.txtItem2SubTotal.Text = order.SubTotalItem2.ToString()
                        Me.cboItem3.Text = order.Item3Name
                        Me.txtItem3UnitPrice.Text = order.UnitPriceItem3.ToString()
                        Me.txtItem3Quantity.Text = order.QuantityItem3.ToString()
                        Me.txtItem3SubTotal.Text = order.SubTotalItem3.ToString()
                        Me.cboItem4.Text = order.Item4Name
                        Me.txtItem4UnitPrice.Text = order.UnitPriceItem4.ToString()
                        Me.txtItem4Quantity.Text = order.QuantityItem4.ToString()
                        Me.txtItem4SubTotal.Text = order.SubTotalItem4.ToString()
                        Me.txtCleaningTotal.Text = order.CleaningTotal.ToString()
                        Me.txtTaxRate.Text = order.TaxRate.ToString()
                        Me.txtTaxAmount.Text = order.TaxAmount.ToString()
                        Me.txtNetPrice.Text = order.OrderTotal.ToString()
                    End If
                Next
            Else
                MsgBox("There is no cleaning order with that receipt number!")
                Exit Sub
            End If
    End Sub
  17. Execute the application and try opening a previously saved order
  18. Close the form and return to your programming environment
 

Previous Copyright © 2004-2011 FunctionX