me123
me123

Reputation: 101

Why am I getting a "the name does not exist in the current context" error on this C# program?

Why am I getting a "the name does not exist in the current context" error on this C# program?

Here is my class file:

namespace Exercise8
{
    class Park
    {    
        Park aPark = new Park();

        private string name;

        public string Name
        {
            get
            {    
                name = Console.ReadLine();
                return name;
            }
            set
            {
            }
        }

        private string location;

        public string Location
        {
            get
            {
                location = Console.ReadLine();
                return location;
            }
            set
            {
            }
        }

        private string facilityType;

        public string FacilityType
        {
            get
            {
                facilityType = Console.ReadLine();
                return facilityType;
            }
            set
            {
            }
        }

        private string facilitiesAvailable;

        public string FacilitiesAvailable
        {
            get
            {
                facilitiesAvailable = Console.ReadLine();
                return facilitiesAvailable;
            }
            set
            {
            }
        }

        private double fee;
        public string sFee;

        public double Fee
        {
            get
            {
                fee = double.Parse(sFee);
                sFee = Console.ReadLine();
        

        return fee;
            }
            set
            {
            }
        }

        private int noOfEmployees;
        public string sNoOfEmployees;

        public int NoOfEmployees
        {
            get
            {
                noOfEmployees = int.Parse(sNoOfEmployees);
                sNoOfEmployees = Console.ReadLine();
                return noOfEmployees;
            }
            set
            {
            }
        }
       
        //variables for Cost Per Visitor.
        //noVisitors will be used in Calculate Revenue

        public double costPerVisitor;
        public double annualBudget = 200000;
        public double noVisitors = 10000;

        public double CalculateCostPerVisitor(double annualBudget, double noOfVisitors)
        {
            //divide annual budget by number of visitors
            return costPerVisitor = annualBudget / noVisitors;
        }
    
        //Calculate Revenue
        public double revenue;

        public double CalculateRevenue(double noOfVisitors,double fee)
        {
            //No of Visitors times Fee
            revenue = noVisitors * fee;
            return revenue;
        }

        public override string ToString()
        {
            return "Name of park: " + this.Name + "\nLocation: " + this.Location + "\nFacility Type: " + this.FacilityType + "\nFacility Fee: " + this.Fee + "\nNumber of employees: " + this.NoOfEmployees +
                "\nNumber of visitors recorded in the past 12 months: " + this.noVisitors + "Annual Budget: " + this.annualBudget;
        }
    }
}

Here is my program:

namespace Exercise8
{
    class Program
    {    
        public static void main (String [] args)
        {       
            //Instantiate Objects

            Park aPark = new Park();

            //Call Methods

            Console.WriteLine("Name of park: " + aPark.Name + "; Location of park: " + aPark.Location + "; Type of park: " + aPark.FacilityType);

            Console.WriteLine("Name of park: " + aPark.Name + "; Location of park: " + aPark.Location + "; Facilities available: " + aPark.FacilitiesAvailable);

            Console.WriteLine("Annual cost per visitor: " + CalculateCostPerVisitor());

            Console.WriteLine("Revenue: " + CalculateRevenue());

            //Below should hopefully return To String
            Console.WriteLine(aPark.ToString()); 
        }    
    }
}

And these are the errors I'm seeing:

The name 'CalculateCostPerVisitor' does not exist in the current context

The name 'CalculateRevenue' does not exist in the current context line

Upvotes: 1

Views: 3896

Answers (3)

p.s.w.g
p.s.w.g

Reputation: 148990

The methods are defined as instance methods on the Park class type, so you need to call the method with a reference to an instance of Park. Additionally, each method takes 2 parameters, so you'd have to provide them at the time you call the methods:

Console.WriteLine("Annual cost per visitor: " + aPark.CalculateCostPerVisitor( ... /* actual parameters here */));

Console.WriteLine("Revenue: " + aPark.CalculateRevenue( ... /* actual parameters here */));

However, since you've defined annualBudget, noOfVisitors, and fee as fields of your Park class, I think it's likely that you never really intended these values to passed in as parameters—or at the very least you're confused about whether these should really be parameters or if the class should calculate the result from the field values.

I'd recommend you remove these parameters, and simply calculate the results from the field values:

public double CalculateCostPerVisitor()
{
    //divide annual budget by number of visitors
    this.costPerVisitor = this.annualBudget / this.noVisitors;
    return this.costPerVisitor;
}

public double CalculateRevenue()
{
    //No of Visitors times Fee
    this.revenue = this.noVisitors * this.fee;
    return this.revenue;
}

...

Console.WriteLine("Annual cost per visitor: " + aPark.CalculateCostPerVisitor());

Console.WriteLine("Revenue: " + aPark.CalculateRevenue());

Not entirely relevant to the question, but there are a few other things wrong (or at least very strange) about your class:

  1. Park aPark = new Park();
    You are creating new instance of Park inside every Park, this is bound to lead to a stack overflow error if you try to create a single instance. You should remove this line from your class file.

  2. name = Console.ReadLine();
    You are reading from the console every time you try to get the value from a property. This is wrong on many levels. All of your properties should just get / set private values and try to do as little work as possible. If you want to allow the user to specify the name in the console, that should be done in your Main method like this:

    aPark.Name = Console.ReadLine();
    
  3. fee = double.Parse(sFee);
    sFee = Console.ReadLine();
    I'm not entirely sure what's going on here, but it's backwards. You need to read the input from the console first, then try to parse it. And again, it should be done in the Main method, look like this:

    string sFee = Console.ReadLine();
    aPark.Fee = double.Parse(sFee);
    
  4. Once you've corrected the properties following the above steps, you can drop the private backing fields and dramatically simplify your code using automatic properties, like this:

    public string Name { get; set; }
    
  5. You should generally avoid public fields. If you keep costPerVisitor as a member of your class, you should probably make it a property, like this:

    public double CostPerVisitor { get; set; }
    

Upvotes: 1

JNL
JNL

Reputation: 4703

Problem 1:

call the method with a reference to an instance of Park

aPark.CalculateCostPerVisitor(argument1, argument2);

Problem 2:

Also, CalculateCostPerVisitor() needs to have two arguments. There is not CalculateCostPerVisitor() method in your class.

You have

public double CalculateCostPerVisitor(double annualBudget, double noOfVisitors)
public double CalculateRevenue(double noOfVisitors,double fee)

Upvotes: 0

Ben Voigt
Ben Voigt

Reputation: 283614

Those methods are defined for objects of type Park, but you're trying to access them from inside Program without qualification or an object of type Park.

For static methods, you would need to qualify them with the class name. But these methods are not static.

For instance methods, you need to call them on a particular instance. You have an instance aPark, but you aren't using it when you tried to call its methods.

Upvotes: 2

Related Questions