所以,我有一些类的做法,并且我已经很容易被类困住了一个星期左右。我不知道如何从一个班级转到另一个班级,但是我的任务要求我这样做。C#'Basics.Product'不包含'ProductGroup'的定义
我已经用英文翻译了我的代码。
using System;
namespace Basics
{
// Sinun koodi tulee tähän
public class Product
{
string name;
decimal price;
public Product(string name, object ProductGroup, decimal price)
{
this.Price = price;
this.Name = name;
}
public Product(string name, object ProductGroup)
{
this.Name = name;
}
public decimal Price
{
get
{
return price;
}
set
{
price = value;
}
}
private string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public string Summary
{
get
{
return this.Name + this.ProductGroup + this.Price;
}
}
}
public class ProductGroup
{
string name;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public ProductGroup(string name)
{
this.Name = name;
}
}
class Program
{
static void Main()
{
Product[] purchases = new Product[2];
ProductGroup applianceGroup = new ProductGroup("Appliances");
purchases[0] = new Product("Coffeemaker", applianceGroup, 49.90M);
purchases[1] = new Product("Microwave", applianceGroup);
purchases[1].Price = 99.90M;
decimal sum = 0;
foreach (Product t in purchases)
{
sum += t.Price;
Console.WriteLine(t.Summary);
}
Console.WriteLine("Totals {0:f2}", sum);
Console.ReadLine();
}
}
}
这使我的问题该生产线是return this.Name + this.ProductGroup + this.Price;
返回我的错误提前'Basics.Product' does not contain a definition for 'ProductGroup' and no extension method 'ProductGroup' accepting a first argument of type 'Basics.Product' could be found (are you missing a using directive or an assembly reference?
感谢。
(建议)为每个类设置一个独立的* .cs文件并确保它们具有相同的命名空间。编辑@Chris建议^^ – 2014-09-03 09:50:46
@Sebastian:虽然这可能是很好的建议,整洁的代码,你应该澄清,这是你的建议,而不是解决问题(因为这将无济于事)。 – Chris 2014-09-03 09:51:50