Feeds:
Posts
Comments

Archive for November, 2011

If there is a switch statement or a series of if/else statements, then we could use dictionary. For example, “key” is an integer and if “key” == 1 function PerformActionA() is called, if “key” == 2  function PerformActionB() is called and if “key” == 3 function PerformActionC() is called. The following is switch statement for this example: 

            switch (key)

            {

                case 1:

                    //perform action a

                    PerformActionA();

                    break;

                case 2:

                    //perform action b

                    PerformActionB();

                    break;

                case 3:

                    //perform action C

                    PerformActionC();

                    break;

            }

We can simplify the above statement into a dictionary. The following are the steps to do so:

  1. Create a Dictionary: The very first step is to create a dictionary:
    1. TKey: The dictionary key’s type is the type of the switch statement’s argument. For example, if we are comparing strings to determine the case, then the key of the dictionary will be a string. On the other hand if we are comparing integers, then the key is an integer. In the above example since “key” is an integer, then the dictionary’s key is an integer.
    2. TValue: The value’s type will be either “Action” (if functions do not return values) or “Func” (If functions do return values). In the example above, since the functions PerformActionA(), PerformActionB(), and PerformActionC() do not return value, then the type will be “Action”.

                  Therefore for the example above, the dictionary of methods will be:

                  Dictionary<int, Action> methodList = new Dictionary<int, Action>();

  1. Add records to Dictionary: For every case, add the case key value as dictionary’s key and the method name as dictionary’s value. For the example above we’ll have:

                                    methodList.Add(1, PerformActionA);

                                    methodList.Add(2, PerformActionB);

                                    methodList.Add(3, PerformActionC);

  1. Call the Dictionary: To use the dictionary, first check to ensure the key value exists in the dictionary. If the value exists, then the dictionary’s name followed by the key value in brackets and () is the format. For the above example it will be:

                                     int key = 2; 

                                     if (methodList.ContainsKey(key))

                                     {

                                                   methodList[key]();

                                      }

Read Full Post »

Time Value of Money

Name Characteristics Example Financial Calculator
Single Cash Flow
  • One lump sum
  • In a period
Receive $100 a year from now pmt = 0
Multiple Cash Flows
  • Cash flows
  • Several periods
  • Number of periods is known
Receiving $100 first year, $200 second year, and $300 third year If uneven cash flows: calculate one cash flow at a timeEven cash flows: pmt = Cash Flow
Annuity
  • Same cash flow
  • Several periods
  • Number of periods is finite
Receiving $100 per year for 5 years pmt = Cash Flow
Coupon Bond
  • Same cash flow – is calculated using coupon rate
  • Several periods
  • Number of periods is finite
  • Receive a lump sum at the end of period
A bond with 9% coupon rate for 5 years Calculate the first section and the lump sum of 100 separate and add the values together
Perpetuity
  • Same cash flow
  • Several periods
  • Number of periods is infinite
Receiving $100 per year for ever Can not use financial calculator
Growing Perpetuity
  • Cash flows
  • Growing cash flows
  • Several periods
  • Number of periods is infinite
Receiving $100 that grows 5% per year for ever Can not use financial calculator

Read Full Post »