Home

Example Application:
Solas Property Rental

 

Solas Property Rental

Introduction

To support arrays in an application, the .NET Framework provides the array class. The primary characteristic of the array class is that it follows the features, including the strengths and weaknesses, of an array. One of the strengths of an array is that each item can be located by a specific index. One of the weaknesses of an array is that, unlike linked list, the collection of items can neither shrink nor grow.

Because array is a class, it is equipped with properties and methods. Besides the regular features of a class, array is serializable. This means that you can use either binary or SOAP serialization to save the items of an array.

To serialize our list, we will use binary serialization.

 

Practical Learning Practical Learning: Introducing Array Serialization

  1. If you want to follow this exercise, start Microsoft Visual C#
  2. To start a new application, on the main menu, click File -> New Project...
  3. In the Templates list, click Windows Application
  4. Set the Name to SolasPropertyRental1 and click OK
  5. In the Solution Explorer, right-click Form1.cs and click Rename
  6. Type RentalProperties.cs and press Enter
  7. From the Toolbox, click ListView and click the form
  8. While the list view is still selected on the form, in the Properties window, click Columns and click its ellipsis button
  9. 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
  10. Click OK
  11. 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
  12. To create a new form, on the main menu, click Project -> Add Windows Form...
  13. In the Templates list, make sure Windows Form is selected.
    Set the Name to NewProperty and click Add
  14. 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
  15. 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.

Starting the List

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 Practical Learning: Creating an Indexed Property

  1. To create a new class, on the main menu, click Project -> Add Class...
  2. In the Templates list, make sure Class is selected.
    Set the Name to RentalProperty and click Add
  3. 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");
            }
        }
    }
  4. To create a new class, on the main menu, click Project -> Add Class...
  5. In the Templates list, make sure Class is selected.
    Set the Name to PropertyListing and click Add
  6. 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;
            }
        }
    }
  7. 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.

Displaying the List

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 Practical Learning: Displaying the Properties

  1. Display the first form and double-click an empty area of its body
  2. 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();
            }
        }
    }
  3. Return to the form and double-click the New Property button 
  4. 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();
    }
  5. Return to the form and double-click its Close button
  6. Implement it as follows:
     
    private void btnClose_Click(object sender, EventArgs e)
    {
        Close();
    }
  7. Execute the application to see the result
  8. Create a few properties
     
    Solas Property Rental
  9. Close the application and execute it again
 

Home Copyright © 2007 FunctionX, Inc.