Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

interest rate

Status
Not open for further replies.

malola123

Technical User
Feb 16, 2007
17
0
0
CA
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.
}
}





 

newAmount is defined as an instance variable. You cannot use an instance variable inside a static method.

Hint: Move the definition of newAmount inside the static method.

Hint 2: After doing that you will encounter different errors, but you should be able to continue learning as you solve them.

 
public static double InterestCalculator(double amount, double interestRate)

remove static:
public double InterestCalculator(double amount, double interestRate)

create an instance of Interest and reference the instance:
Interest interest = new Interest();
Console.WriteLine(interest.InterestCalculator(userAmount, userRate)); // problem here.

looks like HW put I think you posted enough to get a little help.

Marty
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top