所以,我有一些類的做法,並且我已經很容易被類困住了一個星期左右。我不知道如何從一個班級轉到另一個班級,但是我的任務要求我這樣做。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