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 Forms Application
- Set the Name to SolasPropertyRental1 and click OK
- 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 New
Item...
- In the Templates list, click Windows Form
- 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, click C++ Class and click Add
- Set the Name to CRentalProperty and click Finish
- Change the header file as follows:
#pragma once
using namespace System;
[Serializable]
public ref class CRentalProperty
{
private:
// These are the characteristics of a property
long propCode;
String ^ type;
String ^ cond;
short beds;
double baths;
double val;
public:
property long PropertyCode
{
long get() { return propCode; }
void set(long value) { propCode = value; }
}
property String ^ PropertyType
{
String ^ get() { return type; }
void set(String ^ value) { type = value; }
}
property String ^ PropertyCondition
{
String ^ get() { return cond; }
void set(String ^ value) { cond = value; }
}
property short Bedrooms
{
short get() { return beds; }
void set(short value) { beds = value; }
}
property double Bathrooms
{
double get() { return (baths <= 0) ? 0.00 : baths; }
void set(double value) { baths = value; }
}
property double MonthlyRent
{
double get() { return (val <= 0) ? 0.00 : val; }
void set(double value) { val = value; }
}
// This flag is used to specify whether a property
// still holds the default values or it has
// previously been updated
int CreationFlag;
public:
// This constructor is used to create
// default values for a property
CRentalProperty(void);
virtual String ^ ToString() override;
};
|
- Access the RentalProperty.cpp source file and change it as follows:
#include "StdAfx.h"
#include "RentalProperty.h"
CRentalProperty::CRentalProperty(void)
{
Random ^ rnd = gcnew Random();
propCode = rnd->Next(100000, 999999);
type = L"Unknown";
cond = L"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
String ^ CRentalProperty::ToString()
{
return L"Property #: " + PropertyCode +
L"\nType: " + PropertyType +
L"\nCondition: " + PropertyCondition +
L"\nBedrooms: " + Bedrooms +
L"\nBathrooms: " + Bathrooms +
L"\nMonthly Rent: " + MonthlyRent.ToString("C");
}
|
- To create a new class, on the main menu, click Project -> Add Class...
- In the Templates list, click C++ Class and click Add
- Set the Name to CPropertyListing and click Finish
- Change the header file as follows:
#pragma once
#include "RentalProperty.h"
using namespace System::IO;
using namespace System::Runtime::Serialization;
using namespace System::Runtime::Serialization::Formatters::Binary;
public ref class CPropertyListing
{
public:
array<CRentalProperty ^> ^ props;
property CRentalProperty ^ default[int]
{
CRentalProperty ^ get(int i)
{
if( (i >= 0) && (i < 100) )
return props[i];
else
return nullptr;
}
void set(int i, CRentalProperty ^ value)
{
props[i] = value;
}
}
public:
CPropertyListing(void);
};
|
- In the Class View, right-click CPropertyListing -> Add ->
Function...
- Set the Return Type to void
- Set the Name to SaveProperties and click Finish
- Change the file as follows:
void CPropertyListing::SaveProperties(void)
{
String ^ strFilename = L"Properties.prp";
FileStream ^ fstProperties = gcnew FileStream(strFilename,
FileMode::Create);
BinaryFormatter ^ bfmProperties = gcnew BinaryFormatter;
try {
bfmProperties->Serialize(fstProperties, this->props);
}
catch(ArgumentNullException ^)
{
MessageBox::Show(L"The properties listing is not available");
}
catch(SerializationException ^)
{
MessageBox::Show(L"The listing could not be saved");
}
finally {
fstProperties->Close();
}
}
|
- In the Class View, right-click CPropertyListing -> Add ->
Function...
- Set the Return Type to void
- Set the Name to CreateDefaultListing and click Finish
- Change the file as follows:
void CPropertyListing::CreateDefaultListing(void)
{
Random ^ rndNumber = gcnew Random;
for(int i = 0; i < 100; i++)
{
CRentalProperty ^ rental = gcnew CRentalProperty;
rental->PropertyCode = rndNumber->Next(100000, 999999);
rental->PropertyType = L"Unknown";
rental->PropertyCondition = L"Unknown";
rental->Bedrooms = 0;
rental->Bathrooms = 0;
rental->MonthlyRent = 0.00;
rental->CreationFlag = 0;
props[i] = rental;
}
SaveProperties();
}
|
- In the Class View, right-click CPropertyListing -> Add ->
Function...
- Set the Return Type to void
- Set the Name to OpenProperties and click Finish
- Change the file as follows:
void CPropertyListing::OpenProperties(void)
{
String ^ strFilename = L"Properties.prp";
FileStream ^ fstProperties = nullptr;
BinaryFormatter ^ bfmProperties = nullptr;
// If the list of properties had already been created
// then open it
if( File::Exists(strFilename) )
{
try {
fstProperties = gcnew FileStream(strFilename,
FileMode::Open);
bfmProperties = gcnew BinaryFormatter;
this->props =
dynamic_cast<array<CRentalProperty ^>^>(
bfmProperties->Deserialize(fstProperties));
}
catch(ArgumentNullException ^)
{
MessageBox::Show(
L"The properties listing is not available");
}
catch(SerializationException ^)
{
MessageBox::Show(L"The listing could not be opened");
}
finally
{
fstProperties->Close();
}
}
else
return;
}
|
- Scroll up in the file and change the constructor as follows:
CPropertyListing::CPropertyListing(void)
{
props = gcnew array<CRentalProperty ^>(100);
// 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(L"Properties.prp") )
{
CreateDefaultListing();
}
// Since we have a file that holds the list of properties
// open it and store the properties in our array
OpenProperties();
}
|
- Save all
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
- Scroll up in the list and, under the #pragma once list, type:
#include "PropertyListing.h"
#include "NewProperty.h"
|
- On top of the Load event, create a new method and implement the Load event as follows:
void ShowProperties(void)
{
CPropertyListing ^ properties = gcnew CPropertyListing();
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(L"C"));
}
}
}
System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)
{
ShowProperties();
}
|
- Return to the form and double-click the New Property button
- Implement its Click event as follows:
System::Void btnNewProperty_Click(System::Object^ sender,
System::EventArgs^ e)
{
NewProperty ^ dlgProperty = gcnew NewProperty;
if( dlgProperty->ShowDialog() == ::DialogResult::OK )
{
CPropertyListing ^ listing = gcnew CPropertyListing;
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(L"Invalid Property Type");
}
try {
listing->props[i]->PropertyCondition =
dlgProperty->cboPropertyConditions->Text;
}
catch(FormatException ^)
{
MessageBox::Show(L"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(L"You cannot create a new property."
L"You can only modify or replace an existing one.");
}
ShowProperties();
}
|
- Return to the form and double-click its Close button
- Implement it as follows:
System::Void btnClose_Click(System::Object^ sender, System::EventArgs^ e)
{
Close();
}
|
- Execute the application to see the result
- Create a few properties
- Close the application and execute it again
|
|