Home

Department Store: Object Serialization

 

Introduction to Serialization

In traditional file processing supported by most compiled languages such as C#, you learn to create values from primitive types (int, double, char, etc) and save them to a medium. Many libraries such as MFC, VCL, and the .NET Framework provide built-in classes you can use to save a variable of your class as if the variable had been created from a primitive data type.

Object serialization consists of saving the state of any object as a whole. Serialization makes it possible to easily create a file-based database since you are able to save and restore the object the same way you would use file processing of primitive types.

Practical Learning Practical Learning: Introducing File-Based Databases

  1. Start a new Windows Forms Application named DepartmentStore3
  2. To add a new form, on the main menu, click Project -> Add Windows Form...
  3. In the Templates list of the Add New Item dialog box, make sure Windows Form is selected.
    Set the Name to NewStoreItem and press Enter
  4. Design the form as follows:
     
    Control Name Text Other Properties
    Label   Item #:  
    TextBox txtItemNumber    
    Label   Item Name:  
    TextBox txtItemName    
    Label   Size:  
    TextBox txtSize    
    Label   Unit Price:  
    TextBox txtUnitPrice   AlignText: Right
    Button btnAddItem Add Item  
    Button btnClose Close  
    Form   AcceptButton: btnAddItem
    MaximizeBox: False
    StartPosition: CenterScreen
     
  5. Arrange the Tab Order (View . Tab Order) so the Item # text box is the last in the sequence:
     
    New Store Item - Tab Order
  6. Double-click an unoccupied area of the form and implement its Click event as follows:
     
    Dim lstStoreItems As ArrayList
    
    Private Sub NewStoreItem_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            lstStoreItems = New ArrayList
    End Sub
  7. In the Class Name combo box, select btnClose
  8. In the Method Name combo box, select Click
  9. Implement the event as follows:
     
    Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click
            Close()
    End Sub
  10. Display the first form (Form1.vb [Design])
  11. Add a button to it with the following properties:
    Text: New Store Item
    Name: btnNewStoreItem
  12. Double-click the New Store Item button and implement its Click event as follows:
     
    Private Sub btnNewStoreItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNewStoreItem.Click
            Dim frmNewItem As New NewStoreItem
    
            frmNewItem.Show()
    End Sub
  13. Add a new Windows Form
  14. Set its Name to StoreInventory and press Enter
  15. Design the form as follows:
     
    Department Store - Inventory
    Control Name Text Other Properties
    DataGrid     Auto Format: Colorful 3
    Button bntLoad Load  
    Button btnClose Close  
    Form     AcceptButton: bntLoad
    MaximizeBox: False
    StartPosition: CenterScreen
  16. Double-click the Close button and implement its Click event as follows:
     
    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
            Close()
    End Sub
  17. Display the first form (Form1.vb [Design])
  18. Add a button to it with the following properties:
    Text: Store Inventory
    Name: btnStoreInventory
  19. Double-click the Store Inventory button
  20. Implement its Click event as follows:
     
    Private Sub btnStoreInventory_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStoreInventory.Click
            Dim frmInventory As New StoreInventory
    
            frmInventory.Show()
    End Sub
  21. Add a new Windows Form and name it CustomerOrder
  22. Design the form as follows:
     
    Department Store - Customer Order
     
    Control Name Text Other Properties
    Label   Item #  
    Label   Description  
    Label   Size  
    Label   Unit Price  
    Label   Qty  
    Label   Sub-Total  
    TextBox txtItemNumber1    
    TextBox txtDescription1    
    TextBox txtSize1    
    TextBox txtUnitPrice1 0.00 TextAlign: Right
    TextBox txtQuantity1 0 TextAlign: Right
    TextBox txtSubTotal1 0.00 TextAlign: Right
    TextBox txtItemNumber2    
    TextBox txtDescription2    
    TextBox txtSize2    
    TextBox txtUnitPrice2 0.00 TextAlign: Right
    TextBox txtQuantity2 0 TextAlign: Right
    TextBox txtSubTotal2 0.00 TextAlign: Right
    TextBox txtItemNumber3    
    TextBox txtDescription3    
    TextBox txtSize3    
    TextBox txtUnitPrice3 0.00 TextAlign: Right
    TextBox txtQuantity3 0 TextAlign: Right
    TextBox txtSubTotal3 0.00 TextAlign: Right
    TextBox txtItemNumber4    
    TextBox txtDescription4    
    TextBox txtSize4    
    TextBox txtUnitPrice4 0.00 TextAlign: Right
    TextBox txtQuantity4 0 TextAlign: Right
    TextBox txtSubTotal4 0.00 TextAlign: Right
    TextBox txtItemNumber5    
    TextBox txtDescription5    
    TextBox txtSize5    
    TextBox txtUnitPrice5 0.00 TextAlign: Right
    TextBox txtQuantity5 0 TextAlign: Right
    TextBox txtSubTotal5 0.00 TextAlign: Right
    TextBox txtItemNumber6    
    TextBox txtDescription6    
    TextBox txtSize6    
    TextBox txtUnitPrice6 0.00 TextAlign: Right
    TextBox txtQuantity6 0 TextAlign: Right
    TextBox txtSubTotal6 0.00 TextAlign: Right
    TextBox txtItemNumber7    
    TextBox txtDescription7    
    TextBox txtSize7    
    TextBox txtUnitPrice7 0.00 TextAlign: Right
    TextBox txtQuantity7 0 TextAlign: Right
    TextBox txtSubTotal7 0.00 TextAlign: Right
    TextBox txtItemNumber8    
    TextBox txtDescription8    
    TextBox txtSize8    
    TextBox txtUnitPrice8 0.00 TextAlign: Right
    TextBox txtQuantity8 0 TextAlign: Right
    TextBox txtSubTotal8 0.00 TextAlign: Right
    Label   This order will be saved in  
    DateTimePicker dtpSaleDate   Format: Custom
    CustomFormat: ddd dd MMM yyyy
    Label   Total Order:  
    TextBox txtTotalOrder 0.00 TextAlign: Right
    Button btnSave Save and Start a New Order  
    Button btnClose Close  
     
  23. Double-click the Close button and implement its Click event as follows:
     
    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
            Close()
    End Sub
  24. Display the first form (Form1.vb [Design])
  25. Add a button to it with the following properties:
    Text: New Customer Order
    Name: btnNewOrder
  26. Double-click the New Customer Order button
  27. Implement its Click event as follows:
     
    Private Sub btnNewOrder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNewOrder.Click
            Dim frmOrder As New CustomerOrder
    
            frmOrder.Show()
    End Sub
  28. Add a new Windows Form and name it DailySales
  29. Design the form as follows:
     
    Control Name Text Other Properties
    Label   View Sales For  
    DateTimePicker dtpSaleDate   Format: Custom
    CustomFormat: ddd dd MMM yyyy
    DataGrid     Auto Format: Colorful 3
    Button btnClose Close  
    Form     MaximizeBox: False
    StartPosition: CenterScreen
  30. Double-click the Close button and implement its Click event as follows:
     
    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
            Close()
    End Sub
  31. Display the first form (Form1.vb [Design])
  32. Add a button to it with the following properties:
    Text: View Daily Sales
    Name: btnDailySales
  33. Double-click the View Daily Sales button
  34. Implement its Click event as follows:
     
    Private Sub btnDailySales_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDailySales.Click
            Dim frmSales As New DailySales
    
            frmSales.Show()
    End Sub
  35. Return to the main form. Add a new button to it and set its properties as follows:
    Text: Close
    Name: btnClose

     
    Department Store - Switchboard
  36. Double-click the Close button and implement its Click event as follows:
     
    Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click
            End
    End Sub
  37. Save all
 

Binary Serialization

The .NET Framework provides all means of serializing any managed class through a concept referred to as binary serialization. To proceed with object serialization, you must first specify the object you would use. As with any other program, you can either use one of the classes built-in the .NET Framework or you can use your own programmer-created class. If you decide to use your own class, you can specify whether the whole class or just some parts of the class can be saved.

Binary serialization consists of saving an object. This object is usually a class that either you create or is provided through the .NET Framework. For a class to be serializable, it must be marked with the Serializable attribute. This means that, when checking the MSDN documentation, any class you see with this attribute is already configured to be serialized. This is the case for almost all list-oriented classes such as Array, ArrayList, etc, including many of the classes we will use when we start studying ADO .NET and relational databases.

If you create your own class and want to be able to save values from its variables, you can (must) mark the class with the Serializable attribute. To do this, type [Serializable] before starting to create the class.

The Serializable attribute informs the compiler that values from the class can be serialized. When creating a serializable class, you have the ability to specify whether all of the member variables of the class can be serialized or just some of them.

Practical Learning Practical Learning: Creating a Serializable Class

  1. To add a new project, on the main menu, click File -> Add Project -> New Project...
  2. In the Project Types list of the Add New Project dialog box, make sure Visual C# Projects is selected
    In the Templates list, click Class Library
  3. Change the Name to StoreItemR2
     
    Add New Project
  4. Click OK
  5. Change the file as follows:
     
    <Serializable()> Public Class CStoreItem
        Private nbr As String
        Private nm As String
        Private sz As String
        Private uprice As Double
    
        Public Sub New()
            nbr = ""
            nm = ""
            sz = ""
            uprice = 0.0
        End Sub
    
        Public Sub New(ByVal number As String, ByVal name As String, _
                       ByVal size As String, ByVal price As Double)
            nbr = number
            nm = name
            size = sz
            uprice = price
        End Sub
    
        Public Property ItemNumber() As String
            Get
                Return nbr
            End Get
            Set(ByVal Value As String)
                nbr = Value
            End Set
        End Property
    
        Public Property ItemName() As String
            Get
                Return nm
            End Get
            Set(ByVal Value As String)
                nm = Value
            End Set
        End Property
    
        Public Property ItemSize() As String
            Get
                Return sz
            End Get
            Set(ByVal Value As String)
                sz = Value
            End Set
        End Property
    
        Public Property UnitPrice() As Double
            Get
                Return uprice
            End Get
            Set(ByVal Value As Double)
                uprice = Value
            End Set
        End Property
    End Class
  6. To create the library, in the Solution Explorer, right-click the StoreItemR2 node and click Build
  7. To get a reference of this library to the department store project, in Solution Explorer, under DepartmentStore3, right-click References and click Add Reference...
  8. In the Add Reference dialog box, click the Projects property page and make sure the StoreItemR2 project is selected:
     
    Add Reference
  9. Click Select and click OK
  10. Change the Load event of the New Store Item form as follows:
     
    Private Sub NewStoreItem_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            lstStoreItems = New ArrayList
    
            Dim tmeNow As Date = Now
    
            ' Get ready to create a random number
            Dim strItemNumber As String = "000000"
    
            Dim mls As Integer = tmeNow.Millisecond
            ' Generate two random numbers between 100 and 999
            Dim rndNumber As New Random(mls)
            Dim NewNumber1 As Integer = rndNumber.Next(100, 999)
            Dim NewNumber2 As Integer = rndNumber.Next(100, 999)
            ' Create an item number from the random numbers
            strItemNumber = NewNumber1.ToString() + "-" + NewNumber2.ToString()
    
            ' Display the created item number in the Item # text box
            Me.txtItemNumber.Text = strItemNumber
    End Sub
  11. Display the Customer Order form. Right-click it and click View Code
  12. In the top section of the file, type Imports StoreItemR2 as the first line of the file
  13. Create the following function above the End Class line:
     
    Private Function LocateStoreItem(ByVal ItemNumber As String) As StoreItemR2.CStoreItem
            Dim item As New StoreItemR2.CStoreItem
            Dim i As Integer
    
            ' Get a list of all items in the store
            For i = 0 To lstAvailableItems.Count Step 1
                ' Get a reference to the current item
                item = lstAvailableItems(i)
    
                ' If the Item Number of the current item matches the argument,
                ' then return it
                If item.ItemNumber = ItemNumber Then
                    Return item
                End If
            Next
    
            ' If the item was not found, then return nothing;
            Return Nothing
        End Function
  14. In the same way, create the following function above the End Class line:
     
    Private Sub CalculateTotalOrder()
            Dim subTotal1 As Double
            Dim subTotal2 As Double
            Dim subTotal3 As Double
            Dim subTotal4 As Double
            Dim subTotal5 As Double
            Dim subTotal6 As Double
            Dim subTotal7 As Double
            Dim subTotal8 As Double
            Dim orderTotal As Double
    
            ' Retrieve the value of each sub total
            subTotal1 = CDbl(Me.txtSubTotal1.Text)
            subTotal2 = CDbl(Me.txtSubTotal2.Text)
            subTotal3 = CDbl(Me.txtSubTotal3.Text)
            subTotal4 = CDbl(Me.txtSubTotal4.Text)
            subTotal5 = CDbl(Me.txtSubTotal5.Text)
            subTotal6 = CDbl(Me.txtSubTotal6.Text)
            subTotal7 = CDbl(Me.txtSubTotal7.Text)
            subTotal8 = CDbl(Me.txtSubTotal8.Text)
    
            ' Calculate the total value of the sub totals
            orderTotal = subTotal1 + subTotal2 + subTotal3 + _
                                 subTotal4 + subTotal5 + subTotal6 + _
                                subTotal7 + subTotal8
    
            ' Display the total order in the appropriate text box
            Me.txtTotalOrder.Text = orderTotal.ToString("F")
    End Sub
  15. In the Class Name combo box, select txtItemNumber1
  16. In the Method Name combo box, select Leave and implement the event as follows:
     
    Private Sub txtItemNumber1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtItemNumber1.Leave
            Dim strItemNumber As String = Me.txtItemNumber1.Text
            Dim objItem As New CStoreItem
    
            objItem = LocateStoreItem(strItemNumber)
    
            If Not IsNothing(objItem) Then
                Me.txtDescription1.Text = objItem.ItemName
                Me.txtSize1.Text = objItem.ItemSize
                Me.txtUnitPrice1.Text = objItem.UnitPrice.ToString("F")
                Me.txtQuantity1.Text = "1"
                Me.txtSubTotal1.Text = objItem.UnitPrice.ToString("F")
    
                CalculateTotalOrder()
            Else
    
                Me.txtItemNumber1.Text = ""
                Me.txtDescription1.Text = ""
                Me.txtSize1.Text = ""
                Me.txtUnitPrice1.Text = "0.00"
                Me.txtQuantity1.Text = "0"
                Me.txtSubTotal1.Text = "0.00"
            End If
    End Sub
  17. In the Class Name combo box, select txt ItemNumber2
  18. In the Method Name combo box, select Leave and implement the event as follows:
     
    Private Sub txtItemNumber2_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtItemNumber2.Leave
            Dim strItemNumber As String = Me.txtItemNumber2.Text
            Dim objItem As New CStoreItem
    
            objItem = LocateStoreItem(strItemNumber)
    
            If Not IsNothing(objItem) Then
                Me.txtDescription2.Text = objItem.ItemName
                Me.txtSize2.Text = objItem.ItemSize
                Me.txtUnitPrice2.Text = objItem.UnitPrice.ToString("F")
                Me.txtQuantity2.Text = "1"
                Me.txtSubTotal2.Text = objItem.UnitPrice.ToString("F")
    
                CalculateTotalOrder()
            Else
    
                Me.txtItemNumber2.Text = ""
                Me.txtDescription2.Text = ""
                Me.txtSize2.Text = ""
                Me.txtUnitPrice2.Text = "0.00"
                Me.txtQuantity2.Text = "0"
                Me.txtSubTotal2.Text = "0.00"
            End If
    End Sub
  19. In the Class Name combo box, select txt ItemNumber3
  20. In the Method Name combo box, select Leave and implement the event as follows:
     
    Private Sub txtItemNumber3_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtItemNumber3.Leave
            Dim strItemNumber As String = Me.txtItemNumber3.Text
            Dim objItem As New CStoreItem
    
            objItem = LocateStoreItem(strItemNumber)
    
            If Not IsNothing(objItem) Then
                Me.txtDescription3.Text = objItem.ItemName
                Me.txtSize3.Text = objItem.ItemSize
                Me.txtUnitPrice3.Text = objItem.UnitPrice.ToString("F")
                Me.txtQuantity3.Text = "1"
                Me.txtSubTotal3.Text = objItem.UnitPrice.ToString("F")
    
                CalculateTotalOrder()
            Else
    
                Me.txtItemNumber3.Text = ""
                Me.txtDescription3.Text = ""
                Me.txtSize3.Text = ""
                Me.txtUnitPrice3.Text = "0.00"
                Me.txtQuantity3.Text = "0"
                Me.txtSubTotal3.Text = "0.00"
            End If
    End Sub
  21. In the Class Name combo box, select txt ItemNumber4
  22. In the Method Name combo box, select Leave and implement the event as follows:
     
    Private Sub txtItemNumber4_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtItemNumber4.Leave
            Dim strItemNumber As String = Me.txtItemNumber4.Text
            Dim objItem As New CStoreItem
    
            objItem = LocateStoreItem(strItemNumber)
    
            If Not IsNothing(objItem) Then
                Me.txtDescription4.Text = objItem.ItemName
                Me.txtSize4.Text = objItem.ItemSize
                Me.txtUnitPrice4.Text = objItem.UnitPrice.ToString("F")
                Me.txtQuantity4.Text = "1"
                Me.txtSubTotal4.Text = objItem.UnitPrice.ToString("F")
    
                CalculateTotalOrder()
            Else
    
                Me.txtItemNumber4.Text = ""
                Me.txtDescription4.Text = ""
                Me.txtSize4.Text = ""
                Me.txtUnitPrice4.Text = "0.00"
                Me.txtQuantity4.Text = "0"
                Me.txtSubTotal4.Text = "0.00"
            End If
    End Sub
  23. In the Class Name combo box, select txt ItemNumber5
  24. In the Method Name combo box, select Leave and implement the event as follows:
     
    Private Sub txtItemNumber5_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtItemNumber5.Leave
            Dim strItemNumber As String = Me.txtItemNumber5.Text
            Dim objItem As New CStoreItem
    
            objItem = LocateStoreItem(strItemNumber)
    
            If Not IsNothing(objItem) Then
                Me.txtDescription5.Text = objItem.ItemName
                Me.txtSize5.Text = objItem.ItemSize
                Me.txtUnitPrice5.Text = objItem.UnitPrice.ToString("F")
                Me.txtQuantity5.Text = "1"
                Me.txtSubTotal5.Text = objItem.UnitPrice.ToString("F")
    
                CalculateTotalOrder()
            Else
    
                Me.txtItemNumber5.Text = ""
                Me.txtDescription5.Text = ""
                Me.txtSize5.Text = ""
                Me.txtUnitPrice5.Text = "0.00"
                Me.txtQuantity5.Text = "0"
                Me.txtSubTotal5.Text = "0.00"
            End If
    End Sub
  25. In the Class Name combo box, select txt ItemNumber6
  26. In the Method Name combo box, select Leave and implement the event as follows:
     
    Private Sub txtItemNumber6_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtItemNumber6.Leave
            Dim strItemNumber As String = Me.txtItemNumber6.Text
            Dim objItem As New CStoreItem
    
            objItem = LocateStoreItem(strItemNumber)
    
            If Not IsNothing(objItem) Then
                Me.txtDescription6.Text = objItem.ItemName
                Me.txtSize6.Text = objItem.ItemSize
                Me.txtUnitPrice6.Text = objItem.UnitPrice.ToString("F")
                Me.txtQuantity6.Text = "1"
                Me.txtSubTotal6.Text = objItem.UnitPrice.ToString("F")
    
                CalculateTotalOrder()
            Else
    
                Me.txtItemNumber6.Text = ""
                Me.txtDescription6.Text = ""
                Me.txtSize6.Text = ""
                Me.txtUnitPrice6.Text = "0.00"
                Me.txtQuantity6.Text = "0"
                Me.txtSubTotal6.Text = "0.00"
            End If
    End Sub
  27. In the Class Name combo box, select txt ItemNumber7
  28. In the Method Name combo box, select Leave and implement the event as follows:
     
    Private Sub txtItemNumber7_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtItemNumber7.Leave
            Dim strItemNumber As String = Me.txtItemNumber7.Text
            Dim objItem As New CStoreItem
    
            objItem = LocateStoreItem(strItemNumber)
    
            If Not IsNothing(objItem) Then
                Me.txtDescription7.Text = objItem.ItemName
                Me.txtSize7.Text = objItem.ItemSize
                Me.txtUnitPrice7.Text = objItem.UnitPrice.ToString("F")
                Me.txtQuantity7.Text = "1"
                Me.txtSubTotal7.Text = objItem.UnitPrice.ToString("F")
    
                CalculateTotalOrder()
            Else
    
                Me.txtItemNumber7.Text = ""
                Me.txtDescription7.Text = ""
                Me.txtSize7.Text = ""
                Me.txtUnitPrice7.Text = "0.00"
                Me.txtQuantity7.Text = "0"
                Me.txtSubTotal7.Text = "0.00"
            End If
    End Sub
  29. In the Class Name combo box, select txt ItemNumber8
  30. In the Method Name combo box, select Leave and implement the event as follows:
     
    Private Sub txtItemNumber8_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtItemNumber8.Leave
            Dim strItemNumber As String = Me.txtItemNumber8.Text
            Dim objItem As New CStoreItem
    
            objItem = LocateStoreItem(strItemNumber)
    
            If Not IsNothing(objItem) Then
                Me.txtDescription8.Text = objItem.ItemName
                Me.txtSize8.Text = objItem.ItemSize
                Me.txtUnitPrice8.Text = objItem.UnitPrice.ToString("F")
                Me.txtQuantity8.Text = "1"
                Me.txtSubTotal8.Text = objItem.UnitPrice.ToString("F")
    
                CalculateTotalOrder()
            Else
    
                Me.txtItemNumber8.Text = ""
                Me.txtDescription8.Text = ""
                Me.txtSize8.Text = ""
                Me.txtUnitPrice8.Text = "0.00"
                Me.txtQuantity8.Text = "0"
                Me.txtSubTotal8.Text = "0.00"
            End If
    End Sub
  31. At the end of the above procedures, create the following method:
     
    Private Function CalculateSubTotal(ByVal strPrice As String, ByVal strQuantity As String) As Double
            Dim qty As Integer = 0
            Dim unitPrice As Double = 0.0
            Dim subTotal As Double
    
            Try
                ' Get the quantity of the current item
                qty = CInt(strQuantity)
            Catch ex As FormatException
                MsgBox("The value you provided for the quantity of the item is invalid" _
                    + vbCrLf + "Please try again")
            End Try
    
            Try
                ' Get the unit price of the current item
                unitPrice = CDbl(strPrice)
            Catch ex As FormatException
                MsgBox("The unit price you provided for item is invalid" _
                    + vbCrLf + "Please try again")
            End Try
    
            ' Calculate the current sub total
            subTotal = qty * unitPrice
    
            Return subTotal
    End Function
  32. In the Class Name combo box, select txtQuantity1
  33. In the Method Name combo box, select Leave and implement the event as follows:
     
    Private Sub txtQuantity1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtQuantity1.Leave
            Dim strUnitPrice As String
            Dim strQty As String
            Dim subTotal As Double
    
            strUnitPrice = Me.txtUnitPrice1.Text
            strQty = Me.txtQuantity1.Text
            subTotal = Me.CalculateSubTotal(strUnitPrice, strQty)
    
            ' Display the new sub total in the corresponding text box
            Me.txtSubTotal1.Text = subTotal.ToString("F")
            ' Update the order
            CalculateTotalOrder()
    End Sub
  34. In the Class Name combo box, select txtQuantity2
  35. In the Method Name combo box, select Leave and implement the event as follows:
     
    Private Sub txtQuantity2_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtQuantity2.Leave
            Dim strUnitPrice As String
            Dim strQty As String
            Dim subTotal As Double
    
            strUnitPrice = Me.txtUnitPrice2.Text
            strQty = Me.txtQuantity2.Text
            subTotal = Me.CalculateSubTotal(strUnitPrice, strQty)
    
            ' Display the new sub total in the corresponding text box
            Me.txtSubTotal2.Text = subTotal.ToString("F")
            ' Update the order
            CalculateTotalOrder()
    End Sub
  36. In the Class Name combo box, select txtQuantity3
  37. In the Method Name combo box, select Leave and implement the event as follows:
     
    Private Sub txtQuantity3_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtQuantity3.Leave
            Dim strUnitPrice As String
            Dim strQty As String
            Dim subTotal As Double
    
            strUnitPrice = Me.txtUnitPrice3.Text
            strQty = Me.txtQuantity3.Text
            subTotal = Me.CalculateSubTotal(strUnitPrice, strQty)
    
            ' Display the new sub total in the corresponding text box
            Me.txtSubTotal3.Text = subTotal.ToString("F")
            ' Update the order
            CalculateTotalOrder()
    End Sub
  38. In the Class Name combo box, select txtQuantity4
  39. In the Method Name combo box, select Leave and implement the event as follows:
     
    Private Sub txtQuantity4_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtQuantity4.Leave
            Dim strUnitPrice As String
            Dim strQty As String
            Dim subTotal As Double
    
            strUnitPrice = Me.txtUnitPrice4.Text
            strQty = Me.txtQuantity4.Text
            subTotal = Me.CalculateSubTotal(strUnitPrice, strQty)
    
            ' Display the new sub total in the corresponding text box
            Me.txtSubTotal4.Text = subTotal.ToString("F")
            ' Update the order
            CalculateTotalOrder()
    End Sub
  40. In the Class Name combo box, select txtQuantity5
  41. In the Method Name combo box, select Leave and implement the event as follows:
     
    Private Sub txtQuantity5_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtQuantity5.Leave
            Dim strUnitPrice As String
            Dim strQty As String
            Dim subTotal As Double
    
            strUnitPrice = Me.txtUnitPrice5.Text
            strQty = Me.txtQuantity5.Text
            subTotal = Me.CalculateSubTotal(strUnitPrice, strQty)
    
            ' Display the new sub total in the corresponding text box
            Me.txtSubTotal5.Text = subTotal.ToString("F")
            ' Update the order
            CalculateTotalOrder()
    End Sub
  42. In the Class Name combo box, select txtQuantity6
  43. In the Method Name combo box, select Leave and implement the event as follows:
     
    Private Sub txtQuantity6_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtQuantity6.Leave
            Dim strUnitPrice As String
            Dim strQty As String
            Dim subTotal As Double
    
            strUnitPrice = Me.txtUnitPrice6.Text
            strQty = Me.txtQuantity6.Text
            subTotal = Me.CalculateSubTotal(strUnitPrice, strQty)
    
            ' Display the new sub total in the corresponding text box
            Me.txtSubTotal6.Text = subTotal.ToString("F")
            ' Update the order
            CalculateTotalOrder()
    End Sub
  44. In the Class Name combo box, select txtQuantity7
  45. In the Method Name combo box, select Leave and implement the event as follows:
     
    Private Sub txtQuantity7_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtQuantity7.Leave
            Dim strUnitPrice As String
            Dim strQty As String
            Dim subTotal As Double
    
            strUnitPrice = Me.txtUnitPrice7.Text
            strQty = Me.txtQuantity7.Text
            subTotal = Me.CalculateSubTotal(strUnitPrice, strQty)
    
            ' Display the new sub total in the corresponding text box
            Me.txtSubTotal7.Text = subTotal.ToString("F")
            ' Update the order
            CalculateTotalOrder()
    End Sub
  46. In the Class Name combo box, select txtQuantity8
  47. In the Method Name combo box, select Leave and implement the event as follows:
     
    Private Sub txtQuantity8_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtQuantity8.Leave
            Dim strUnitPrice As String
            Dim strQty As String
            Dim subTotal As Double
    
            strUnitPrice = Me.txtUnitPrice8.Text
            strQty = Me.txtQuantity8.Text
            subTotal = Me.CalculateSubTotal(strUnitPrice, strQty)
    
            ' Display the new sub total in the corresponding text box
            Me.txtSubTotal8.Text = subTotal.ToString("F")
            ' Update the order
            CalculateTotalOrder()
    End Sub
  48. Save all

The Process of Serializing

To support serialization of an object, the .NET Framework provides the BinaryFormatter class from the System.Runtime.Serialization.Formatters.Binary namespace. This class is derived from Object but implements the IRemotingFormatter and the IFormatter interfaces. One of the methods of this class is called Serialize. This method is overloaded with two versions. The syntax of the first version is:

Overloads Public Overridable Sub Serialize( _
   ByVal serializationStream As Stream, _
   ByVal graph As Object _
) Implements IFormatter.Serialize

The first argument of this method is a stream object that will carry the details of the streaming process. It must be a class derived from Stream. The second argument is a variable that carries the value(s) to be saved. It can be an instance of a class you created or a variable declared from a .NET Framework serializable class.

Practical Learning Practical Learning: Serializing an Object

  1. Display the New Store Item form. Right-click an empty area of its body and click View Code
  2. In the Class Name combo box, select (NewStoreItem Events)
  3. In the Method Name combo box, select Deactivate
  4. Implement the event as follows:
     
    Private Sub NewStoreItem_Deactivate(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Deactivate
            Dim strFilename As String = "StoreItems.sti"
            Dim fStream As New FileStream(strFilename, FileMode.Create)
            Dim binFormat As New BinaryFormatter
    
            Try
                binFormat.Serialize(fStream, Me.lstStoreItems)
            Catch anex As ArgumentNullException
                MsgBox("The inventory could not be accessed")
            Catch serex As SerializationException
                MsgBox("The inventory could not be saved")
            Finally
                fStream.Close()
            End Try
    End Sub
  5. Save all

Object Deserialization

The opposite of serialization is deserialization. Deserialization is the process of retrieving an object that was serialized. When this is done, an exact copy of the object that was saved is created and restored as the origin.

To support deserialization, the BinaryFormatter class provides the Deserialize() method, which is overloaded in two versions. The syntax of the first version:

Overloads Public Overridable Function Deserialize( _
   ByVal serializationStream As Stream _
) As Object Implements IFormatter.Deserialize

This method as argument the object that holds the file and the details on how the file will be opened.

Practical Learning Practical Learning: Deserializing an Object

  1. In the Method Name combo box, select Activated
  2. In the top section of the file, type the following lines
     
    imports System.IO
    imports System.Runtime.Serialization
    imports System.Runtime.Serialization.Formatters.Binary
    Imports StoreItemR2
  3. Implement the Activated event as follows:
     
    Dim lstStoreItems As ArrayList
    
    Private Sub NewStoreItem_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated
            ' This file holds the items sold in the store
            Dim strFilename As String
            Dim fStream As FileStream
    
            strFilename = "StoreItems.sti"
    
            If File.Exists(strFilename) Then
                Try
                    ' Create a stream of store items
                    fStream = New FileStream(strFilename, FileMode.Open)
    
                    Dim binFormat As New BinaryFormatter
                    lstStoreItems = binFormat.Deserialize(fStream)
                Catch anex As ArgumentNullException
                    MsgBox("The inventory could not be accessed")
                Catch ex As SerializationException
                    MsgBox("The application failed to retrieve the inventory")
                Finally
    
                    fStream.Close()
                End Try
    	 else
                Return
            End If
    End Sub
  4. In the Class Name combo box, select btnAddItem
  5. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub btnAddItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddItem.Click
            Dim objItem As New StoreItemR2.CStoreItem
    
            ' Make sure the record is complete before attempting to add it
            If Me.txtItemNumber.Text = "" Then Exit Sub
            If Me.txtItemName.Text = "" Then Exit Sub
            If Me.txtUnitPrice.Text = "" Then Exit Sub
    
            ' Now the record seems to be ready: build it
            objItem.ItemNumber = Me.txtItemNumber.Text
            objItem.ItemName = Me.txtItemName.Text
            objItem.ItemSize = Me.txtSize.Text
            objItem.UnitPrice = CDbl(Me.txtUnitPrice.Text)
            ' Add the record
            Me.lstStoreItems.Add(objItem)
    
            ' Get ready to create a unique item number
            Dim tmeNow As Date = Now
    
            ' Get ready to create a unique item number
            Dim FoundUniqueNumber As Boolean = False
            Dim strItemNumber As String = "000000"
    
            Dim mls As Integer = tmeNow.Millisecond
            ' Generate two random numbers between 100 and 999
            Dim rndNumber As New Random(mls)
            Dim NewNumber1 As Integer = rndNumber.Next(100, 999)
            Dim NewNumber2 As Integer = rndNumber.Next(100, 999)
            ' Create an item number from the random numbers
            strItemNumber = NewNumber1.ToString() + "-" + NewNumber2.ToString()
    
            ' Reset the form for a new record
            Me.txtItemNumber.Text = strItemNumber
            Me.txtItemName.Text = ""
            Me.txtSize.Text = ""
            Me.txtUnitPrice.Text = ""
            Me.txtItemName.Focus()
    End Sub
  6. Execute the application and create a few items (let the computer generate the item numbers):
     
    Item Name Size Unit Price
    Women Cashmere Lined Glove 8 115.95
    Men Trendy Jacket Medium 45.85
    Women Stretch Flare Jeans Petite 27.75
    Women Belted Sweater L 15.95
    Girls Classy Handbag One Size 95.95
    Women Casual Dress Shoes 9.5M 45.95
    Boys Hooded Sweatshirt M (7/8) 15.95
    Girls Velour Dress 10 12.55
    Women Lace Desire Panty M 7.15
    Infant Girls Ballerina Dress 12M 22.85
    Men Classic Pinstripe Suit 38 145.95
  7. Close the forms and return to your programming environment
  8. Display the Store Inventory form and double-click its Load button
  9. In the top section of the file, type the following lines and the first lines of the file:
     
    Imports System.IO
    Imports System.Runtime.Serialization
    Imports System.Runtime.Serialization.Formatters.Binary
  10. Implement the Click event as follows:
     
    Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
            Dim lstItems As ArrayList = New ArrayList
            Dim strFilename As String
            Dim fStream As FileStream
            Dim binFormat As BinaryFormatter
            strFilename = "StoreItems.sti"
    
            If File.Exists(strFilename) Then
                fStream = New FileStream(strFilename, FileMode.Open)
    
                Try
                    lstItems = binFormat.Deserialize(fStream)
    
                    Me.dataGrid1.DataSource = Nothing
                    Me.dataGrid1.DataSource = lstItems
                Catch anex As ArgumentNullException
                    MsgBox("The inventory could not be accessed")
                Catch serex As SerializationException
                    MsgBox("The application failed to retrieve the inventory")
                Finally
                    fStream.Close()
                End Try
            End If
    End Sub
  11. Execute the application
  12. Click the Store Inventory button and click the Load button to display the inventory
     
    Department Store - Inventory
  13. Close the forms and return to your programming environment
  14. Display the Customer Order form. Right-click an unoccupied area of its body and click View Code
    In the Method Name combo box, select Activated
  15. In the top section of the file, type the following lines and the first lines of the file:
     
    Imports System.IO
    Imports System.Runtime.Serialization
    Imports System.Runtime.Serialization.Formatters.Binary
  16. Implement the event as follows:
     
    Private Sub CustomerOrder_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated
            lstAvailableItems = New ArrayList
            Dim strFilename As String
            Dim binFormat As BinaryFormatter = New BinaryFormatter
    
            ' This file holds the items sold in the store
            strFilename = "StoreItems.sti"
            Dim fStream As FileStream
    
            If File.Exists(strFilename) Then
                Try
                    ' Create a stream of store items
                    fStream = New FileStream(strFilename, FileMode.Open)
                    lstAvailableItems = binFormat.Deserialize(fStream)
                Catch anex As ArgumentNullException
                    MsgBox("The inventory could not be accessed")
                Catch serex As SerializationException
                    MsgBox("The application failed to retrieve the inventory")
                Finally
                    fStream.Close()
                End Try
    	 else
                Exit Sub
            End If
    End Sub
  17. To add a new project, in the Solution Explorer, right-click Solution Department3 (2 Projects), position the mouse on Add and click New Project...
  18. In the Project Types list of the Add New Project dialog box, make sure Visual Basic Projects is selected
    In the Templates list, click Class Library
  19. Change the Name to SalesInventory and click OK
  20. Change the file as follows:
     
    <Serializable()> Public Class CSaleInventory
        Private nbr As String
        Private nm As String
        Private sz As String
        Private uprice As Double
        Private iQty As Integer
        Private stotal As Double
    
        Public Sub New()
            nbr = ""
            nm = ""
            sz = ""
            uprice = 0.0
            iQty = 0
            stotal = 0.0
        End Sub
    
        Public Sub New(ByVal number As String, ByVal name As String, _
                       ByVal size As String, ByVal price As Double, _
                       ByVal qty As Integer, ByVal total As Double)
            nbr = number
            nm = name
            size = sz
            uprice = price
            iQty = qty
            stotal = total
        End Sub
    
        Public Property ItemNumber() As String
            Get
                Return nbr
            End Get
            Set(ByVal Value As String)
                nbr = Value
            End Set
        End Property
    
        Public Property ItemName() As String
            Get
                Return nm
            End Get
            Set(ByVal Value As String)
                nm = Value
            End Set
        End Property
    
        Public Property ItemSize() As String
            Get
                Return sz
            End Get
            Set(ByVal Value As String)
                sz = Value
            End Set
        End Property
    
        Public Property UnitPrice() As Double
            Get
                Return uprice
            End Get
            Set(ByVal Value As Double)
                uprice = Value
            End Set
        End Property
    
        Public Property Quantity() As Integer
            Get
                Return iQty
            End Get
            Set(ByVal Value As Integer)
                iQty = Value
            End Set
        End Property
    
        Public Property SubTotal() As Double
            Get
                Return stotal
            End Get
            Set(ByVal Value As Double)
                stotal = Value
            End Set
        End Property
    End Class
  21. To create the library, in the Solution Explorer, right-click the SalesInventory node and click Build
  22. To get a reference of this library to the department store project, in Solution Explorer, under DepartmentStore3, right-click References and click Add Reference...
  23. In the Projects property page, click SalesInventory and click Select
  24. Click OK
  25. Display the Customer Order form
  26. Double-click the Save And Start New Order button and implement its Click event as follows:
     
    Private Sub btnSaveOrder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveOrder.Click
            Dim dteCurrent As DateTime = Me.dtpSaleDate.Value
            Dim strFilename As String
            Dim lstCurrentSoldItems As ArrayList
            Dim curOrder As SalesInventory.CSaleInventory
            Dim fStream As FileStream
            Dim binFormat As BinaryFormatter = New BinaryFormatter
    
            Dim month As Integer = dteCurrent.Month
            Dim day As Integer = dteCurrent.Day
            Dim year As Integer = dteCurrent.Year
    
            strFilename = month.ToString() + "-" + day.ToString() + _
                                   "-" + year.ToString() + ".dly"
            lstCurrentSoldItems = New ArrayList
    
            ' If this is not the first order of this file, then first open the file
            ' and store its orders in a temporary list
            If File.Exists(strFilename) Then
    
                Try
                    ' Create a stream of store items
                    fStream = New FileStream(strFilename, FileMode.Open, FileAccess.Read, FileShare.Read)
    
                    lstCurrentSoldItems = binFormat.Deserialize(fStream)
                Catch anex As ArgumentNullException
    
                    MsgBox("The inventory could not be accessed")
                Catch serex As SerializationException
                    MsgBox("The application failed to retrieve the inventory")
                Finally
                    fStream.Close()
                End Try
            End If
    
            ' Whether the file existed already or not, add the current items to the list
            If Me.txtItemNumber1.Text <> "" Then
                curOrder = New SalesInventory.CSaleInventory
                curOrder.ItemNumber = Me.txtItemNumber1.Text
                curOrder.ItemName = Me.txtDescription1.Text
                curOrder.ItemSize = Me.txtSize1.Text
                curOrder.UnitPrice = CDbl(Me.txtUnitPrice1.Text)
                curOrder.Quantity = CInt(Me.txtQuantity1.Text)
                curOrder.SubTotal = CDbl(Me.txtSubTotal1.Text)
                lstCurrentSoldItems.Add(curOrder)
            End If
    
            If Me.txtItemNumber2.Text <> "" Then
                curOrder = New SalesInventory.CSaleInventory
                curOrder.ItemNumber = Me.txtItemNumber2.Text
                curOrder.ItemName = Me.txtDescription2.Text
                curOrder.ItemSize = Me.txtSize2.Text
                curOrder.UnitPrice = CDbl(Me.txtUnitPrice2.Text)
                curOrder.Quantity = CInt(Me.txtQuantity2.Text)
                curOrder.SubTotal = CDbl(Me.txtSubTotal2.Text)
                lstCurrentSoldItems.Add(curOrder)
            End If
    
            If Me.txtItemNumber3.Text <> "" Then
                curOrder = New SalesInventory.CSaleInventory
                curOrder.ItemNumber = Me.txtItemNumber3.Text
                curOrder.ItemName = Me.txtDescription3.Text
                curOrder.ItemSize = Me.txtSize3.Text
                curOrder.UnitPrice = CDbl(Me.txtUnitPrice3.Text)
                curOrder.Quantity = CInt(Me.txtQuantity3.Text)
                curOrder.SubTotal = CDbl(Me.txtSubTotal3.Text)
                lstCurrentSoldItems.Add(curOrder)
            End If
    
            If Me.txtItemNumber4.Text <> "" Then
                curOrder = New SalesInventory.CSaleInventory
                curOrder.ItemNumber = Me.txtItemNumber4.Text
                curOrder.ItemName = Me.txtDescription4.Text
                curOrder.ItemSize = Me.txtSize4.Text
                curOrder.UnitPrice = CDbl(Me.txtUnitPrice4.Text)
                curOrder.Quantity = CInt(Me.txtQuantity4.Text)
                curOrder.SubTotal = CDbl(Me.txtSubTotal4.Text)
                lstCurrentSoldItems.Add(curOrder)
            End If
    
            If Me.txtItemNumber5.Text <> "" Then
                curOrder = New SalesInventory.CSaleInventory
                curOrder.ItemNumber = Me.txtItemNumber5.Text
                curOrder.ItemName = Me.txtDescription5.Text
                curOrder.ItemSize = Me.txtSize5.Text
                curOrder.UnitPrice = CDbl(Me.txtUnitPrice5.Text)
                curOrder.Quantity = CInt(Me.txtQuantity3.Text)
                curOrder.SubTotal = CDbl(Me.txtSubTotal5.Text)
                lstCurrentSoldItems.Add(curOrder)
            End If
    
            If Me.txtItemNumber6.Text <> "" Then
                curOrder = New SalesInventory.CSaleInventory
                curOrder.ItemNumber = Me.txtItemNumber6.Text
                curOrder.ItemName = Me.txtDescription6.Text
                curOrder.ItemSize = Me.txtSize6.Text
                curOrder.UnitPrice = CDbl(Me.txtUnitPrice6.Text)
                curOrder.Quantity = CInt(Me.txtQuantity6.Text)
                curOrder.SubTotal = CDbl(Me.txtSubTotal6.Text)
                lstCurrentSoldItems.Add(curOrder)
            End If
    
            If Me.txtItemNumber7.Text <> "" Then
                curOrder = New SalesInventory.CSaleInventory
                curOrder.ItemNumber = Me.txtItemNumber7.Text
                curOrder.ItemName = Me.txtDescription7.Text
                curOrder.ItemSize = Me.txtSize7.Text
                curOrder.UnitPrice = CDbl(Me.txtUnitPrice7.Text)
                curOrder.Quantity = CInt(Me.txtQuantity7.Text)
                curOrder.SubTotal = CDbl(Me.txtSubTotal7.Text)
                lstCurrentSoldItems.Add(curOrder)
            End If
    
            If Me.txtItemNumber8.Text <> "" Then
                curOrder = New SalesInventory.CSaleInventory
                curOrder.ItemNumber = Me.txtItemNumber8.Text
                curOrder.ItemName = Me.txtDescription8.Text
                curOrder.ItemSize = Me.txtSize8.Text
                curOrder.UnitPrice = CDbl(Me.txtUnitPrice8.Text)
                curOrder.Quantity = CInt(Me.txtQuantity8.Text)
                curOrder.SubTotal = CDbl(Me.txtSubTotal8.Text)
                lstCurrentSoldItems.Add(curOrder)
            End If
    
            fStream = New FileStream(strFilename, FileMode.Create, FileAccess.Write, FileShare.None)
    
            Try
                binFormat.Serialize(fStream, lstCurrentSoldItems)
            Catch anex As ArgumentNullException
                MessageBox.Show("The customer order could not be accessed")
            Catch serex As SerializationException
                MessageBox.Show("The customer order could not be saved")
                End
            Finally
                fStream.Close()
            End Try
    
            ' Reset the order
            Me.txtItemNumber1.Text = ""
            Me.txtItemNumber2.Text = ""
            Me.txtItemNumber3.Text = ""
            Me.txtItemNumber4.Text = ""
            Me.txtItemNumber5.Text = ""
            Me.txtItemNumber6.Text = ""
            Me.txtItemNumber7.Text = ""
            Me.txtItemNumber8.Text = ""
    
            Me.txtItemNumber1_Leave(sender, e)
            Me.txtItemNumber2_Leave(sender, e)
            Me.txtItemNumber3_Leave(sender, e)
            Me.txtItemNumber4_Leave(sender, e)
            Me.txtItemNumber5_Leave(sender, e)
            Me.txtItemNumber6_Leave(sender, e)
            Me.txtItemNumber7_Leave(sender, e)
            Me.txtItemNumber8_Leave(sender, e)
    
            Me.txtTotalOrder.Text = "0.00"
            Me.dtpSaleDate.Value = DateTime.Today
    
            Me.txtItemNumber1.Focus()
    End Sub 
  27. Execute the application
  28. Click the Store Inventory button and click the Load button to display the inventory
  29. Keep the inventory form opened and from the main form, click the Customer Order button
  30. Process a few orders and save them at different dates. Write on a piece of paper the dates the orders are saved
  31. Close the forms and return to your programming environment
  32. Display the Daily Sales form
  33. Double-click the Date Time Picker control on it
  34. In the top section of the file, type the following lines and the first lines of the file:
     
    Imports System.IO
    Imports System.Runtime.Serialization
    Imports System.Runtime.Serialization.Formatters.Binary
  35. Implement the event as follows:
     
    Private Sub dtpSaleDate_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtpSaleDate.ValueChanged
            Dim dteCurrent As DateTime
            Dim strFilename As String
            Dim lstDaySoldItems As ArrayList = New ArrayList
            Dim binFormat As BinaryFormatter = New BinaryFormatter
    
            dteCurrent = Me.dtpSaleDate.Value
            Dim month As Integer = dteCurrent.Month
            Dim day As Integer = dteCurrent.Day
            Dim year As Integer = dteCurrent.Year
            strFilename = month.ToString() + "-" + day.ToString() + "-" + year.ToString() + ".dly"
    
            ' Find out if the file exists, that is, if some orders were placed on that day
            If File.Exists(strFilename) Then
                Dim fStream As FileStream = New FileStream(strFilename, FileMode.Open, FileAccess.Read, FileShare.Read)
    
                Try
                    binFormat = New BinaryFormatter
                    lstDaySoldItems = binFormat.Deserialize(fStream)
    
                    Me.dataGrid1.DataSource = Nothing
                    Me.dataGrid1.DataSource = lstDaySoldItems
                Catch anex As ArgumentNullException
                    MsgBox("The inventory could not be accessed")
                Catch serex As SerializationException
                    MsgBox("The application failed to retrieve the inventory")
                Finally
                    fStream.Close()
                End Try
            Else
                Me.dataGrid1.DataSource = Nothing
            End If
        End Sub
  36. Execute the application and test all objects
 

Home Copyright © 2004-2012, FunctionX