Hi,
im a novice programmer, who is learning c#;
I am creating a code where i calculate interest over a 10 year period after a user is prompted for an initial investment amount and an interest rate. Im having a problem where the error states:
An object reference is required for the nonstatic field, method, or property 'Interest.InterestCalculator(double, double)' on line 68.
here is the code:
using System;
class Interest
{
int i = 0;
double newAmount;
public static double InterestCalculator(double amount, double interestRate)
{
for (int i = 0; i <= 9; i++)
{
newAmount = amount * interestRate;
amount += newAmount;
}
return newAmount;
}
public override string ToString()
{
string result = "";
result += string.Format(" Year {0} Total = {1:F2}", i + 1, newAmount);
return result;
}
}
class Test
{
public static void Main()
{
Console.WriteLine("Interest Calculator");
double userAmount;
double userRate;
string userInput;
Console.Write("Enter an initial investment amount: ");
userInput = Console.ReadLine();
userAmount = double.Parse(userInput);
if (userAmount < 0.00d || userAmount > 100000.00d)
{
Console.WriteLine("ERROR. Please enter again.");
userInput = Console.ReadLine();
userAmount = double.Parse(userInput);
}
Console.Write("Enter annual interest rate: ");
userInput = Console.ReadLine();
userRate = double.Parse(userInput);
if (userRate < 0.00d || userRate > 1.00d)
{
Console.WriteLine("ERROR. Please enter again.");
userInput = Console.ReadLine();
userRate = double.Parse(userInput);
}
Console.WriteLine(Interest.InterestCalculator(userAmount, userRate)); // problem here.
}
}
im a novice programmer, who is learning c#;
I am creating a code where i calculate interest over a 10 year period after a user is prompted for an initial investment amount and an interest rate. Im having a problem where the error states:
An object reference is required for the nonstatic field, method, or property 'Interest.InterestCalculator(double, double)' on line 68.
here is the code:
using System;
class Interest
{
int i = 0;
double newAmount;
public static double InterestCalculator(double amount, double interestRate)
{
for (int i = 0; i <= 9; i++)
{
newAmount = amount * interestRate;
amount += newAmount;
}
return newAmount;
}
public override string ToString()
{
string result = "";
result += string.Format(" Year {0} Total = {1:F2}", i + 1, newAmount);
return result;
}
}
class Test
{
public static void Main()
{
Console.WriteLine("Interest Calculator");
double userAmount;
double userRate;
string userInput;
Console.Write("Enter an initial investment amount: ");
userInput = Console.ReadLine();
userAmount = double.Parse(userInput);
if (userAmount < 0.00d || userAmount > 100000.00d)
{
Console.WriteLine("ERROR. Please enter again.");
userInput = Console.ReadLine();
userAmount = double.Parse(userInput);
}
Console.Write("Enter annual interest rate: ");
userInput = Console.ReadLine();
userRate = double.Parse(userInput);
if (userRate < 0.00d || userRate > 1.00d)
{
Console.WriteLine("ERROR. Please enter again.");
userInput = Console.ReadLine();
userRate = double.Parse(userInput);
}
Console.WriteLine(Interest.InterestCalculator(userAmount, userRate)); // problem here.
}
}