Search

Categories

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Send mail to the author(s) E-mail

# Thursday, December 24, 2009

Symbolic constants vs Enumerated Types'

These constants are handy when cheking on the setter (as can use an int range)

    public const int APARTMENT = 1;
    public const int COMMERCIAL = 2;
    public const int HOME = 3;
 
    public int BuildingType
    {
        get { return buildingType; }
        set
        {
            if (value >= APARTMENT && value <= HOME)
                buildingType = value;
        }
    }
This keyword
public clsBuilding()
    {
        address = "Not closed yet";
    }
 
    public clsBuilding(string addr, decimal price, decimal payment, decimal tax, decimal insur, DateTime date, int type)
        : this() // good practise to call initial constructor first..second constructor will overwrite address = "Not closed yet"
    {
        if (addr.Equals("") == false)
            address = addr;
        purchasePrice = price;
        monthlyPayment = payment;
        taxes = tax;
        insurance = insur;
        datePurchased = date;
        buildingType = type;
    }
Base keyword
    public clsCommercial(string addr, decimal price, decimal payment,
                        decimal tax, decimal insur, DateTime date, int type) :
                        base(addr, price, payment, tax, insur, date, type)
    {
        buildingType = type;   // Commercial type from base
    }

this is calling the bases’ constructor.

can also call base methods from the derived (child) class.

Polymorphism

essentially this means we can send the same message to a group of different classes and each class will know how to respond correctly. eg

image

        lstMessages.Items.Add(myApt.RemoveSnow());
        lstMessages.Items.Add(myComm.RemoveSnow());
        lstMessages.Items.Add(myHome.RemoveSnow());

clsApt and clsComm have overridden methods and respond with different phone numbers.  clsHome doesn’t have a RemoveSnow method and responds as above.

Abstract Classes

To prevent instantiation of the base class ie if it makes no sense.

public abstract class clsDeciduous
{
 
Virtual and Override
The method may be overridden in a derived class.
 
    public virtual string RemoveSnow()
    {
        return whichType[buildingType] + ": No snow removal service available.";
    }

And in the derived class:

public override string RemoveSnow()
    {
        return "Commercial: Call Acme Snow Plowing: 803.234.5566";
    }

To prevent further inheritence use public sealed class.