Click here to Skip to main content
15,121,030 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this class

C#
public class MonthlyPayment
    {
        public int Month { get; set; }
        public double InitialBalance { get; set; }
        public double MonthlyRate { get; set; }
        public double Interest { get; set; }
        public double Principal { get; set; }
        public double Insurance { get; set; }
        public double Commission { get; set; }
        public double TotalMonthly{ get; set; }
        public double CreditBalance { get; set; }
    }


And this list of the first manually created item

C#
public class CreditDataContext
    {
        public List<MonthlyPayment> MonthlyPayments { get; set; }
        public void LoadMonthlyPayments()
        {
            MonthlyPayments = new List<MonthlyPayment>();
            MonthlyPayment payment = null;
            payment = new MonthlyPayment()
            {
                Month = 0,
                InitialBalance = 0.00,
                MonthlyRate = 0.00,
               Interest = 0.00,
                Principal = 0.00,
                Insurance = 0.00,
                Commission = 200.00,
                TotalMonthly = 200.00,
                Credit Balance = 10000.00f
            };
            MonthlyPayments.Add(payment);


How do I create a method of class MonthlyPayment to calculate all the details of a monthly payment?
I should use a for(i=1,...,12)?

What I have tried:

I tried to create the function ,but it does work.
C#
public MonthlyPayment  Calculate(int i)
        {
            MonthlyPayment x = new MonthlyPayment();
            x.Month = i;
            x.InitialBalance =10.000f ;
            x.Interest = ((9.75 /100) / 12) * x.InitialBalance;
            double v = 1 + x.Interest;
            x.MonthlyRate = (x.InitialBalance * x.Interest) / (1 - Math.Pow(v, -12));
            x.Principal = x.MonthlyRate - x.Interest;
            x.Insurance = (0.10 / 100) * x.InitialBalance;
            x.Commission = (0.04 / 100) * x.InitialBalance;
            x.TotalMonthly = x.Interest + x.Insurance + x.MonthlyRate;
            x.CreditBalance = x.InitialBalance - x.MonthlyRate;}
        }
Posted
Updated 2-May-21 12:27pm

1 solution

Why are you creating a new instance in your method, which gets thrown away at the end for the method when you return?

Since your Calculate method is non-static, it has an instance to work with: the current instance (which is also known as this - but in this example you don't need to use it explicitly).

For example, if I create a Fraction class:
C#
public class Fraction
   {
   public int Numerator;
   public int Denominator;
   public Fraction (int numerator, int denominator)
      {
      Numerator = numerator;
      Denominator = denominator;
      }
   }
I can add a method to evaluate the fraction as a decimal value:
C#
public double Evaluate()
   {
   return (double) Numerator / (double) Demoninator;
   }

This uses the values that were set when the class instance was created.

Your code can do the same: set the class instance values directly when you call your method:
C#
public MonthlyPayment  alculate(int i)
    {
    Month = i;
    InitialBalance =10.000f ;
    Interest = ((9.75 /100) / 12) * InitialBalance;
    double v = 1 + x.Interest;
...
    return this;
    }
   
Comments
Member 15180481 3-May-21 11:26am
   
How do I call the function in main()?
OriginalGriff 3-May-21 11:45am
   
You create an instance, and call the function using that ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900