Click here to Skip to main content
15,074,377 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I have the main cs file with the following:
C#
<pre>using System;

namespace Assignment_10
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter Loan Amount: ");
            C1.LoanA = Convert.ToDouble(Console.ReadLine());
            Console.Write("Enter Years: ");
            C1.Years = Convert.ToDouble(Console.ReadLine());
            Console.Write("Enter Interest Rate: ");
            C1.InterestR = Convert.ToDouble(Console.ReadLine());

            C1.PayInterests();
            C1.iMessage();
        }
    }
}

And then the C1 cs file with the following:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Assignment_10
{
    public class C1 : IMyInterface
    {
        private double loanAmount = 0.0;
        public double LoanA
        {
            get
            {
                return loanAmount;
            }
            set
            {
                loanAmount = value;
            }
        }
        private double years = 0.0;
        public double Years
        {
            get
            {
                return years;

            }
            set
            {
                years = value;
            }
        }
        private double interests = 0.0;
        public double Interests
        {
            get
            {
                return interests;
            }
            set
            {
                interests = value;
            }
        }
        private double interestRate = 0.0;
        public double InterestR
        {
            get
            {
                return interestRate;
            }
            set
            {
                interestRate = value;
            }
        }
        public C1(double loanAmount,double years, double interests, double interestRate)
        {
            loanAmount = LoanA;
            years = Years;
            interests = Interests;
            interestRate = InterestR;
        }
        public void PayInterests()
        {
            interests = loanAmount * interestRate * years;
            Console.WriteLine("Total Interests: $" + interests);
        }
        public void iMessage()
        {
            Console.WriteLine("Be Ready!");
        }
    }
}

And finally an instance file that looks like this:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Assignment_10
{
    public interface IMyInterface
    {
        void iMessage();
    }
}

I have tried emailing my teacher for help and he has not given me any assistance on the matter and I have looked online for answers and am having trouble finding what I need. I know that if I made each method and variable static it would work but my teacher said I can not do that.

What I have tried:

I have been searching for solutions everywhere and I have not figured out any solution.
Posted
Updated 6-Apr-21 17:45pm

You never created an instance of your C1 class inside your Main method. That's what it means. You're trying to use a normal instance class like a static class. You can't do that.

You have a constructor on the C1 class that requires a bunch of parameters so you can create an instance of the C1 class.

You have to collect all of your information first, the create an instance of the class, then you can use the properties and methods of the class.

using System;

C#
namespace Assignment_10
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create variables to hold your input data here.
            double variable name;
            ...

            // Prompt for and get your data into the above variables.
            Console.Write("Enter Loan Amount: ");
            C1.LoanA = Convert.ToDouble(Console.ReadLine());      // DO NOT USE C1 HERE!
            
            Console.Write("Enter Years: ");
            C1.Years = Convert.ToDouble(Console.ReadLine());      // OR HERE!

            Console.Write("Enter Interest Rate: ");
            C1.InterestR = Convert.ToDouble(Console.ReadLine());  // OR HERE!

            // Create your C1 instance using the data from above.
            C1 instance = new C1(parameters go here);      // Pass in your parameters from above

            // Use your instance, not the class name!
            instance.PayInterests();
            instance.iMessage();
        }
    }
}
   
Quote:
Compiler Error CS0120
An object reference is required for the nonstatic field, method, or property 'member'

In order to use a non-static field, method, or property, you must first create an object instance. For more information about static methods, see Static Classes and Static Class Members. For more information about creating instances of classes, see Instance Constructors.
your code will compile ...

A not-static Class is a template: you create instances of it by using the 'new operator:
C1 test = new C1(1000.0, 2.4, 0.12, 0.25);
test.PayInterests();
test.iMessage();
Study this: [^]
   

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