Home > Articles > Programming > C#

  • Print
  • + Share This
From the author of

From the author of

Inside the InvestmentController Class

Now let's look at what's inside a class, starting with InvestmentController. First are fields. Most of the fields in this class have private modifiers because I feel like an OO purist today and don't want any other classes to touch them. The default is private, but it doesn't hurt to be explicit. Here are the fields in the InvestmentController class:

     private decimal startingPrincipal;
     private float  interestRate;
     private int   numberOfPeriods;

     private bool  dataChanged = false;

     const  int   periodsPerYear = 12;

The fields start off with a decimal type, which is a floating-point number that is accurate up to about 28 digits. This is good for financial applications. C# also has float and double types. Its integral types range from an 8-bit byte to a 64-bit long; on top of that, they can be signed or unsigned. There is also a bool (true/false) and a Unicode char type. These are all value types that are allocated on the stack.

The periodsPerYear field is a const, which is set at compile time and can't be changed. If you want constant behavior with runtime initialization, you can mark a field with the readonly modifier and initialize it in the class constructor.

Every class has one or more constructors, used to initialize class fields. The InvestmentController class has two constructors. Constructor names are the same as their class, and they don't return values. Here are the constructors for the InvestmentController class:

     public InvestmentController() : this (1.00m, 1.00f, 1)
     {
     }

     public InvestmentController(decimal principal, float rate, int months)
     {
        startingPrincipal = principal;
        interestRate   = rate;
        numberOfPeriods  = months;
     }

The first constructor doesn't take any parameters, and the second takes three. To make sure that an object is initialized properly, the first constructor calls the second with the this operator and three arguments. The this operator refers to the same object where it appears.

C# has a new type of class member called a property. These are sometimes referred to as smart fields because they are used just like fields. Here's an example of a property:

     public decimal Principal
     {
        get
        {
          return startingPrincipal;
        }
        set
        {
          startingPrincipal = value;
          dataChanged    = true;
        }
     }

I'll revisit properties again when explaining how they're used, but now let's look at their structure. The get accessor sets an internal field named startingPrincipal. The set accessor sets the startingPrincipal field to the value assigned to the property and changes the Boolean dataChanged field to true. The value keyword is the value passed to the property. For example, the expression Principal = 15.50 results in the startingPrincipal value being set to 15.50. Likewise, the expression decimal paid = Principal would result in the paid field being set to 15.50, assuming that the value of startingPrincipal was already set to 15.50. By leaving out the get accessor, a property is write-only; when leaving out the set accessor, a property is read-only.

Methods are similar to functions or procedures in other languages. Here's an example:

     public decimal EstimateReturn()
     {
        return Principal*(decimal)Math.Pow((double)(1+Interest/12), (double)Months);
     }

This method doesn't have any parameters, and it returns a decimal value. Principal, Interest, and Months are all properties. Just imagine how the internal representation of class data could change, but this method would remain intact because all the moving around is hidden within the property accessors.

Classes may be static also, which means that they don't have an instance but their members may be used. Such is the case with the Math class. The previous example uses the Pow() method of the Math class to calculate the power of a value. The Math class is an excellent example of why a static class should be created because it's stateless, logic only, and it reduces overhead associated with numerous object instantiations.

  • + Share This
  • 🔖 Save To Your Account