1

为什么我在这个 C# 程序上收到“当前上下文中不存在该名称”错误?

这是我的类文件:

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

这是我的程序:

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()); 
        }    
    }
}

这些是我看到的错误:

当前上下文中不存在名称“CalculateCostPerVisitor”

当前上下文行中不存在名称“CalculateRevenue”

4

3 回答 3

2

这些方法是为 type 对象定义的Park,但您试图从内部访问它们Program而没有资格或 type 对象Park

对于静态方法,您需要使用类名来限定它们。但这些方法都不是static

对于实例方法,您需要在特定实例上调用它们。你有一个 instance aPark,但是当你试图调用它的方法时你没有使用它。

于 2013-09-13T21:07:24.040 回答
1

这些方法被定义为Park类类型上的实例方法,因此您需要使用对Park. 此外,每个方法都需要 2 个参数,因此您必须在调用方法时提供它们:

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

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

但是,由于您已将 、 和 定义annualBudgetnoOfVisitorsfee的字段Park,我认为您可能从未真正打算将这些值作为参数传入 - 或者至少您对这些值是否真的应该是参数或如果该类应该从字段值计算结果。

我建议您删除这些参数,并简单地根据字段值计算结果:

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());

与问题不完全相关,但您的课程还有其他一些错误(或至少非常奇怪):

  1. Park aPark = new Park();
    您正在创建Parkevery的新实例,Park如果您尝试创建单个实例,这势必会导致堆栈溢出错误。你应该从你的类文件中删除这一行。

  2. name = Console.ReadLine();
    每次尝试从属性中获取值时,您都在从控制台读取。这在很多层面上都是错误的。您的所有属性都应该只获取/设置私有值并尝试做尽可能少的工作。如果您想允许用户在控制台中指定名称,则应在您的Main方法中完成,如下所示:

    aPark.Name = Console.ReadLine();
    
  3. fee = double.Parse(sFee);
    sFee = Console.ReadLine();
    我不完全确定这里发生了什么,但它是倒退的。您需要先从控制台读取输入,然后尝试解析它。同样,它应该在Main方法中完成,如下所示:

    string sFee = Console.ReadLine();
    aPark.Fee = double.Parse(sFee);
    
  4. 按照上述步骤更正属性后,您可以删除私有支持字段并使用自动属性显着简化代码,如下所示:

    public string Name { get; set; }
    
  5. 您通常应该避免使用公共字段。如果您保留costPerVisitor为班级的成员,则可能应该将其设为属性,如下所示:

    public double CostPerVisitor { get; set; }
    
于 2013-09-13T21:07:58.403 回答
0

问题1:

通过引用 Park 的实例调用该方法

aPark.CalculateCostPerVisitor(argument1, argument2);

问题2:

此外,CalculateCostPerVisitor()需要有两个参数。你的课上没有CalculateCostPerVisitor()方法。

你有

public double CalculateCostPerVisitor(double annualBudget, double noOfVisitors)
public double CalculateRevenue(double noOfVisitors,double fee)
于 2013-09-13T21:08:10.530 回答