15,304,157 members
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; }
{
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
};

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 11:27am

## Solution 1

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;
}```
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 ...