Foolios

This quote is from the book C#: The Complete Reference.

the methods of a class typically manipulate and provide access to the data of the class. With this in mind, recall that Main( ) in the preceding examples computed the area-per-person by dividing the total area by the number of occupants. While technically correct, this is not the best way to handle this computation. The calculation of area-per-person is something that is best handled by the Building class, itself. The reason for this conclusion is easy to understand: The area-per-person of a building is dependent upon the values in the area and occupants fields, which are encapsulated by Building. Thus, it is possible for the Building class to perform this calculation on its own. Furthermore, by adding this calculation to Building, you prevent each program that uses Building from having to perform this calculation manually. This prevents the unnecessary duplication of code.


I was hoping someone could explain this a little bit more. I still don't get why it makes any difference whether the act of this method is in main or otherwise outside of main as it's own method. The method still exists in the class right Main is in the class. I don't understand what difference this makes to the objects.
Thanks in advance for breaking this down further.

I don't get any of what this line is saying:
Furthermore, by adding this calculation to Building, you prevent each program that uses Building from having to perform this calculation manually. This prevents the unnecessary duplication of code.
How would it be doing it manually


Re: Visual C# Express Edition Understanding of class methods

Frank Boyne

I don't have the book you are quoting from so the following could be complete nonsense.

I'll speculate that main looks something vaguely like this


static void Main(string[] args)
{
Building b1 = new Building ( /* some parameter list */ );

double areaPerPerson;

areaPerPerson = b1.Area / b1.Occupants;
}

The author is making the point that the area-per-person calculation should be encapsulated within the Building class itself and not supplied separately ('manually') by the consumer of the building class (i.e., Main).

Imagine for example that Main now has to calculate area per person for several buildings. The code to do that 'manually' would look like this...


static void Main(string[] args)
{
/* declarations and initialisation ommitted */

areaPerPerson1 = b1.Area / b1.Occupants;

areaPerPerson2 = b2.Area / b2.Occupants;

areaPerPerson3 = b3.Area / b3.Occupants;

. . .

areaPerPerson100 = b100.Area / b100.Occupants;
}

Whereas, if the calculation were embedded within the Building class as an AreaPerPerson property, the code would look like this...


static void Main(string[] args)
{
/* declarations and initialisation ommitted */

areaPerPerson1 = b1.AreaPerperson;

areaPerPerson2 = b2.AreaPerPerson;

areaPerPerson3 = b3.AreaPerPerson;

. . .

areaPerPerson100 = b100.AreaPerPerson;
}

At first glance, this second approach may not look much better than the first - you have one hundred assignment statements in Main either way. But consider what happens if we now need to change the calculation. Let's say the Fire Marshal mandates that 10% of a building's area must be set aside for corridors, stair-wells, emergency exits etc. so that now the calculation becomes


area-per-person = (0.9 * area) / occupants

Under the 'manual' approach, we must modify one hundred lines of code to implement the new formula for each of the 100 buildings. On the other hand, if we have encapsulated the calculation within an AreaPerPerson property of the Building class then we must only modify the one line of code within the AreaPerPerson implementation to effect the change for all 100 buildings,





Re: Visual C# Express Edition Understanding of class methods

Foolios

Thank you so much, that was an excellent explanation.