Question Real Simple Question on Class

BitLost

Active member
Joined
Dec 10, 2016
Messages
35
Programming Experience
Beginner
Okay so I am really new to C#. Previously I have played about in Access, however now I find the limitations of the combined packages I use to host Cloud systems frustrating forcing me to learn C# and ASP.net at the same time.

In my first step I am setting up a class to hold data relating to a swimming pool. Items like volume, area, surface finish and so on. All good no issue.

The thing that is bothering me is about technique more than how to. In Access the thinking is calculate values on the fly. Therefore we wouldn't store area or volume.

Transposing this thinking forward then would you set the value of average depth for example with:

private double aveDepth

or would you work it on demand in something like:

public getAveDepth
{

return (deep + shallow)/2

}

Or doesn't it matter?:disturbed:
 
Firstly, you should have a property for the average depth, not a field or method. Secondly, the property should be read-only. Then, you have two options:

1. Perform the average depth calculation when the values for the maximum and minimum depths are set and store it in a field that gets returned by the property.
2. Have the calculation performed on the fly by the property.

In such a trivial case, I would say that it doesn't really matter. In the case of a complex calculation, I'd probably lean towards option 1 but option 2 would be fine in this case. All I would suggest is that, whichever option you pick, be consistent. If you use option 2 for this trivial calculation then use option 2 for all trivial calculations.
 
Thanks for taking the time to respond.

I found the readonly and set that when I found it. I did say this was my first dabble after simply learning the basics.

I agree it is trivial. However the underlying philosophy is not.

Thanks again for taking the time to answer.
 
Back
Top Bottom