|
Practical Learning: Introducing Array Serialization
|
|
- If you want to follow this exercise, start Microsoft Visual C#
- To start a new application, on the main menu, click File -> New
Project...
- In the Templates list, click Windows Application
- Set the Name to SolasPropertyRental1 and click OK
- In the Solution Explorer, right-click Form1.cs and click Rename
- Type RentalProperties.cs and press Enter
- From the Toolbox, click ListView and click the form
- While the list view is still selected on the form, in the Properties
window, click Columns and click its ellipsis button
- Create the following columns:
| (Name) |
Text |
TextAlign |
Width |
| colPropertyIndex |
# |
Right |
30 |
| colPropertyNumber |
Property # |
|
70 |
| colPropertyType |
Property Type |
|
100 |
| colPropertyCondition |
Condition |
|
80 |
| colBedrooms |
Bedrooms |
Right |
|
| colBathrooms |
Bathrooms |
Right |
70 |
| colMonthlyRent |
Monthly Rent |
Right |
80 |
- Click OK
- Complete the design of the form as follows:
 |
| Control |
Name |
Text |
Other Properties |
| ListView |
lvwProperties |
|
FullRowSelect: True
GridLines: True
View: Details
Anchor: Top, Bottom, Left, Right |
| Button |
btnNewProperty |
New Property |
Anchor: Bottom, Left |
| Button |
btnClose |
Close |
Anchor: Bottom, Right |
|
- To create a new form, on the main menu, click Project -> Add Windows
Form...
- In the Templates list, make sure Windows Form is selected.
Set the Name to NewProperty and click Add
- Design the form as follows:
 |
| Control |
Name |
Text |
Other Properties |
| Label |
|
Property Type: |
|
| Button |
btnOK |
OK |
|
| Combo Box |
cboPropertyTypes |
Unknown |
Items:
Apartment
Single Family
Townhouse
Unknown |
| Button |
btnOK |
OK |
DialogResult: OK |
| Label |
|
Property Condition: |
|
| Combo Box |
cboPropertyConditions |
Unknown |
Items:
Excellent
Good
Bad Shape
Unknown |
| Button |
btnCancel |
Cancel |
DialogResult: Cancel |
| Label |
|
Bedrooms: |
|
| TextBox |
txtBedrooms |
0 |
TextAlign: Right |
| Label |
|
Bathrooms: |
|
| TextBox |
txtBathrooms |
0.00 |
TextAlign: Right |
| Label |
|
Monthly Rent:
|
|
| TextBox |
txtMonthlyRent |
0.00 |
|
| Form |
|
|
FormBorderStyle: FixedDialog
AcceptButton: btnOK
CancelButton: btnCancel
StartPosition: CenterScreen
MaximumBox: False
MinimumBox: False
ShowInTaskbar: False |
|
- Save all
|
Using an Indexed Property |
|
The application we are creating is used by a property rental
company to review its available products. To manage the houses and apartments
to rent, we will create a class that holds a property. Because our application
is list-based, we will create an indexed property in a class so we can access
each property with an index applied to an instance of the that class.
One of the problems with an array is that its list of items
must be established in the beginning, but in our application we want the user to
be able to add an item to the array. In the next section, we will see that, when
the application starts, we create a default list of items but we will still
give the user the ability to create or add items. To solve the problem of
monitoring the items in the list, we will add a flag to each property. When a
default item has not been set, this flag will indicate it. Eventually, when a
user adds an item, we will change this flag to indicate that the property has
been changed from its default values. We could use a Boolean value for this flag to hold a true or a false
result but it is sometimes a little complex to serialize some Boolean values of
a collection. For this reason, we will make the flag a numeric value:
int CreationFlag;
Before a property has been set, this flag will hold a 0 value.
When a property has been set, this flag will become 1.
One of the problems with an array is that, when
creating it, you must specify the number of items in the list. This is
because, from the beginning, the list uses a fixed number of items. This
also implies that you cannot add an item that increases the volume of the
collection. Because the number of items is fixed, after the list has been
created, you can only replace an existing item.
In our application, we need to overcome or at least go
around these limitations of arrays. There are various actions
we can take. To start, when the application is launched, we will create a
list of items but we will fill out each item with insignificant default
values. This simply allows us to create a complete array.
|
Practical Learning: Creating an Indexed Property
|
|
- To create a new class, on the main menu, click Project -> Add Class...
- In the Templates list, make sure Class is selected.
Set the Name to RentalProperty and click Add
- Change the file as follows:
using System;
namespace SolaspublicRental1
{
[Serializable]
public class PropertyRental
{
// These are the characteristics of a public
private long propCode;
private string type;
private string cond;
private short beds;
private double baths;
private double val;
public long PropertyCode
{
get { return propCode; }
set { propCode = value; }
}
public string PropertyType
{
get { return type; }
set { type = value; }
}
public string PropertyCondition
{
get { return cond; }
set { cond = value; }
}
public short Bedrooms
{
get { return beds; }
set { beds = value; }
}
public double Bathrooms
{
get { return (baths <= 0) ? 0.00 : baths; }
set { baths = value; }
}
public double MonthlyRent
{
get { return (val <= 0) ? 0.00 : val; }
set { val = value; }
}
// This flag is used to specify whether a public
// still holds the default values or it has
// previously been updated
public int CreationFlag;
// This constructor is used to create
// default values for a public
public RentalProperty()
{
Random rnd = new Random();
propCode = rnd.Next(100000, 999999);
type = "Unknown";
cond = "Unknown";
beds = 0;
baths = 0.0;
val = 0.00;
CreationFlag = 0;
}
// This method is used to create a string that
// the characteristics of a property
public override string ToString()
{
return "Property #: " + PropertyCode +
"\nType: " + PropertyType +
"\nCondition: " + PropertyCondition +
"\nBedrooms: " + Bedrooms +
"\nBathrooms: " + Bathrooms +
"\nMonthly Rent: " + MonthlyRent.ToString("C");
}
}
}
|
- To create a new class, on the main menu, click Project -> Add Class...
- In the Templates list, make sure Class is selected.
Set the Name to PropertyListing and click Add
- Change the file as follows:
using System;
using System.IO;
using System.Windows.Forms;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace SolasPropertyRental1b
{
public class PropertyListing
{
private PropertyRental[] props = new PropertyRental[100];
public PropertyListing()
{
// Check if the default list of properties has never been created.
// If there is no default list of properties,
// Then create it and save the file
if (!File.Exists("Properties.prp"))
{
CreateDefaultListing();
}
// Since we have a file that holds the list of properties
// open it and store the properties in our array
OpenProperties();
}
public PropertyRental this[int i]
{
get
{
if ((i >= 0) && (i < 100))
return props[i];
else
return null;
}
set
{
props[i] = value;
}
}
public void SaveProperties()
{
string strFilename = "Properties.prp";
FileStream fstProperties = new FileStream(strFilename,
FileMode.Create);
BinaryFormatter bfmProperties = new BinaryFormatter();
try
{
bfmProperties.Serialize(fstProperties, this.props);
}
catch (ArgumentNullException)
{
MessageBox.Show("The properties listing is not available");
}
catch (SerializationException)
{
MessageBox.Show("The listing could not be saved");
}
finally
{
fstProperties.Close();
}
}
public void CreateDefaultListing()
{
Random rndNumber = new Random();
for (int i = 0; i < 100; i++)
{
PropertyRental rental = new PropertyRental();
rental.PropertyCode = rndNumber.Next(100000, 999999);
rental.PropertyType = "Unknown";
rental.PropertyCondition = "Unknown";
rental.Bedrooms = 0;
rental.Bathrooms = 0;
rental.MonthlyRent = 0.00;
rental.CreationFlag = 0;
props[i] = rental;
}
SaveProperties();
}
public void OpenProperties()
{
string strFilename = "Properties.prp";
FileStream fstProperties = null;
BinaryFormatter bfmProperties = null;
// If the list of properties had already been created
// then open it
if (File.Exists(strFilename))
{
try
{
fstProperties = new FileStream(strFilename,
FileMode.Open);
bfmProperties = new BinaryFormatter();
this.props[100] =
(PropertyRental[])bfmProperties.Deserialize(fstProperties);
}
catch (ArgumentNullException)
{
MessageBox.Show("The properties listing is not available");
}
catch (SerializationException)
{
MessageBox.Show("The listing could not be opened");
}
finally
{
fstProperties.Close();
}
}
else
return;
}
}
}
|
- Save the file
|
Adding or Updating an Item |
|
To give the user the ability to add a property to the list,
we will look for an item whose creation flag is set to 0. When we find a flag
with that value, we will consider that its corresponding property has not been
set. Once we find that property, we will replace it with the values of the new
property.
As discussed so far, our array will contain both default and appropriate
properties. Default properties are those that contain unrecognizable values. To
give a good impression of a list of properties that can be added to, there is no
reason to display the properties that have never been updated by the user. We
will show only properties that the user know have good values. To identify these
properties, once again the flag we created in the class of properties will be
helpful. When we want to display the properties, we will check the status or
value of this property. If it is set to 0, we will not display the property.
|
Practical Learning: Displaying the Properties
|
|
- Display the first form and double-click an empty area of its body
- On top of the Load event, create a new method and implement the Load event as follows:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace SolasPropertyRental1b
{
public partial class RentalProperties : Form
{
public RentalProperties()
{
InitializeComponent();
}
private void ShowProperties()
{
PropertyListing properties = new PropertyListing();
properties.OpenProperties();
lvwProperties.Items.Clear();
for (int i = 0; i < 100; i++)
{
int iFlag = properties.props[i].CreationFlag;
if (iFlag != 0)
{
ListViewItem lvi = lvwProperties.Items.Add((i + 1).ToString());
lvi.SubItems.Add(properties.props[i].PropertyCode.ToString());
lvi.SubItems.Add(properties.props[i].PropertyType);
lvi.SubItems.Add(properties.props[i].PropertyCondition);
lvi.SubItems.Add(properties.props[i].Bedrooms.ToString());
lvi.SubItems.Add(properties.props[i].Bathrooms.ToString());
lvi.SubItems.Add(properties.props[i].MonthlyRent.ToString("C"));
}
}
}
private void RentalProperties_Load(object sender, EventArgs e)
{
ShowProperties();
}
}
}
|
- Return to the form and double-click the New Property button
- Implement its Click event as follows:
private void btnNewProperty_Click(object sender, EventArgs e)
{
NewProperty dlgProperty = new NewProperty();
if( dlgProperty.ShowDialog() == DialogResult.OK )
{
PropertyListing listing = new PropertyListing();
for(int i = 0; i < 100; i++)
{
if( listing.props[i].CreationFlag == 0 )
{
try {
listing.props[i].PropertyType =
dlgProperty.cboPropertyTypes.Text;
}
catch(FormatException )
{
MessageBox.Show("Invalid Property Type");
}
try {
listing.props[i].PropertyCondition =
dlgProperty.cboPropertyConditions.Text;
}
catch(FormatException )
{
MessageBox.Show("Invalid Property Condition");
}
try {
listing.props[i].Bedrooms =
short.Parse(dlgProperty.txtBedrooms.Text);
}
catch(FormatException )
{
MessageBox.Show("Invalid Bedroom Value");
}
try {
listing.props[i].Bathrooms =
double.Parse(dlgProperty.txtBathrooms.Text);
}
catch(FormatException )
{
MessageBox.Show("Invalid Bathroom Value");
}
try {
listing.props[i].MonthlyRent =
double.Parse(dlgProperty.txtMonthlyRent.Text);
}
catch(FormatException )
{
MessageBox.Show("Unrecognizable Monthly Rent Value");
}
listing.props[i].CreationFlag = 1;
listing.SaveProperties();
ShowProperties();
return;
}
}
MessageBox.Show("You cannot create a new property." +
"You can only modify or replace an existing one.");
}
ShowProperties();
}
|
- Return to the form and double-click its Close button
- Implement it as follows:
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
|
- Execute the application to see the result
- Create a few properties

- Close the application and execute it again
|
|