![]() |
File-Based Applications: |
Imagine you get a contract or an assignment to create an application used to process business transactions for a car rental company. In this case, we will call it Bethesda Car Rental. If you decide to use then .NET Framework to develop the application, you have various options. You can use file processing for a file-based database. You can proceed with XML to save company files in a format that can be accessed by different types of applications, including the World Wide Web. Or you can create a formal database using Microsoft SQL Server, Borland/Corel Paradox, or MS Access, etc. In this example, we will create our application using regular files. This application assumes that you have some knowledge or a basic understanding of file processing and serialization as it is featured in the .NET Framework but we will be as clear as possible with this exercise and its different parts.
An employee will be anybody who works for this company, whether the person is part of the management or is one of those who process transactions. In a formal company, various pieces of information are provided for each employee. In our example, we will get only the strict minimum.
|
| (Name) | Text | Width |
| colFirstName | First Name | 80 |
| colLastName | Last Name | 80 |
| colTitle | Title | 200 |
| colGender | Gender | 80 |
![]() |
||||||||||||||||
|
![]() |
||||||||||||||||||||||||||||||||||||||||||||||||
|
<Serializable()> Public NotInheritable Class Employee
Public FirstName As String
Public LastName As String
Public Title As String
Public Gender As String
Public Sub New()
FirstName = "Unknown"
LastName = "Unknown"
Title = "N/A"
Gender = "Unknown"
End Sub
Public Sub New(ByVal fname As String, ByVal lname As String, _
ByVal title As String, ByVal sex As String)
FirstName = fname
LastName = lname
title = title
Gender = sex
End Sub
End Class
|

Imports System.IO
Imports System.Runtime.Serialization.Formatters.Soap
Public Class Employees
Inherits System.Windows.Forms.Form
Private lstEmployees As ArrayList
|
Private Sub ShowEmployees()
Dim bcrSoap As SoapFormatter = New SoapFormatter
Dim strFilename As String = "Employees.bcr"
If File.Exists(strFilename) Then
Dim bcrStream As FileStream = New FileStream(strFilename, FileMode.Open, FileAccess.Read, FileShare.Read)
Dim lstEmpl As ArrayList = bcrSoap.Deserialize(bcrStream)
bcrStream.Close()
Dim empl As Employee
lvwEmployees.Items.Clear()
For i As Integer = 0 To lstEmpl.Count - 1
empl = lstEmpl(i)
Dim lviEmployee As ListViewItem = New ListViewItem(empl.FirstName)
lviEmployee.Font = New Font("Georgia", 8, FontStyle.Bold)
If i Mod 2 = 0 Then
lviEmployee.BackColor = Color.Blue
lviEmployee.ForeColor = Color.White
Else
lviEmployee.BackColor = Color.LightBlue
lviEmployee.ForeColor = Color.Blue
End If
lviEmployee.SubItems.Add(empl.LastName)
lviEmployee.SubItems.Add(empl.Title)
lviEmployee.SubItems.Add(empl.Gender)
lvwEmployees.Items.Add(lviEmployee)
Next
End If
End Sub
|
Private Sub Employees_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
lstEmployees = New ArrayList
Dim bcrSoap As SoapFormatter = New SoapFormatter
Dim strFilename As String = "Employees.bcr"
If File.Exists(strFilename) Then
Dim bcrStream As FileStream = New FileStream(strFilename, FileMode.Open, FileAccess.Read, FileShare.Read)
lstEmployees = bcrSoap.Deserialize(bcrStream)
bcrStream.Close()
End If
ShowEmployees()
End Sub
|
Private Sub btnNewEmployee_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNewEmployee.Click
Dim dlgEmpl As NewEmployee = New NewEmployee
If dlgEmpl.ShowDialog() = DialogResult.OK Then
If dlgEmpl.txtLastName.Text = "" Then
MsgBox("You must provide at least a last name to create a new employee")
Exit Sub
Else
Dim strFilename As String = "Employees.bcr"
Dim empl As Employee = New Employee
empl.FirstName = dlgEmpl.txtFirstName.Text
empl.LastName = dlgEmpl.txtLastName.Text
empl.Title = dlgEmpl.txtTitle.Text
empl.Gender = dlgEmpl.cboGenders.Text
lstEmployees.Add(empl)
Dim bcrStream As FileStream = New FileStream(strFilename, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write)
Dim bcrSoap As SoapFormatter = New SoapFormatter
bcrSoap.Serialize(bcrStream, lstEmployees)
bcrStream.Close()
ShowEmployees()
End If
End If
End Sub
|
Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click
Close()
End Sub
|
Private Sub btnEmployees_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEmployees.Click
Dim frmEmpl As Employees = New Employees
frmEmpl.Show()
End Sub
|
|
||||||||||||||||||||||||||||||||
![]() |
|
|
||
| Home | Copyright © 2005 FunctionX, Inc. | Next |
|
|
||