为什么我在此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”
答案 0 :(得分:2)
这些方法是针对Park
类型的对象定义的,但您尝试从Program
内部无限制地访问它们或者Park
类型的对象。
对于静态方法,您需要使用类名限定它们。但这些方法不是static
。
对于实例方法,您需要在特定实例上调用它们。您有一个实例aPark
,但在尝试调用其方法时,您还没有使用它。
答案 1 :(得分:1)
这些方法被定义为Park
类类型的实例方法,因此您需要通过引用Park
的实例来调用该方法。此外,每个方法都有2个参数,因此您必须在调用方法时提供它们:
Console.WriteLine("Annual cost per visitor: " + aPark.CalculateCostPerVisitor( ... /* actual parameters here */));
Console.WriteLine("Revenue: " + aPark.CalculateRevenue( ... /* actual parameters here */));
但是,由于您已将annualBudget
,noOfVisitors
和fee
定义为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());
与这个问题并不完全相关,但是有一些其他问题(或者至少非常奇怪的)关于你的课程:
Park aPark = new Park();
您正在每Park
内创建Park
的新实例,如果您尝试创建单个实例,则必然会导致堆栈溢出错误。您应该从类文件中删除此行。
name = Console.ReadLine();
每次尝试从属性中获取值时,您都在从控制台读取数据。这在许多层面都是错误的。您的所有属性应该只是获取/设置私有值并尝试尽可能少地工作。如果您想允许用户在控制台中指定名称,那么应该在Main
方法中完成,如下所示:
aPark.Name = Console.ReadLine();
fee = double.Parse(sFee);
sFee = Console.ReadLine();
我不完全确定这里发生了什么,但它是倒退的。您需要先从控制台读取输入,然后尝试解析它。同样,它应该在Main
方法中完成,如下所示:
string sFee = Console.ReadLine();
aPark.Fee = double.Parse(sFee);
按照上述步骤更正属性后,您可以删除私有支持字段,并使用自动属性大大简化代码,如下所示:
public string Name { get; set; }
您通常应避免使用公共字段。如果您将costPerVisitor
作为班级成员,则应该将其设为属性,如下所示:
public double CostPerVisitor { get; set; }
答案 2 :(得分:0)
问题1:
通过引用Park
的实例来调用该方法aPark.CalculateCostPerVisitor(argument1, argument2);
问题2:
此外,CalculateCostPerVisitor()
需要有两个参数。您班级中没有CalculateCostPerVisitor()
方法。
你有
public double CalculateCostPerVisitor(double annualBudget, double noOfVisitors)
public double CalculateRevenue(double noOfVisitors,double fee)