2014-09-03 37 views
0

所以,我有一些类的做法,并且我已经很容易被类困住了一个星期左右。我不知道如何从一个班级转到另一个班级,但是我的任务要求我这样做。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?

感谢。

+0

(建议)为每个类设置一个独立的* .cs文件并确保它们具有相同的命名空间。编辑@Chris建议^^ – 2014-09-03 09:50:46

+1

@Sebastian:虽然这可能是很好的建议,整洁的代码,你应该澄清,这是你的建议,而不是解决问题(因为这将无济于事)。 – Chris 2014-09-03 09:51:50

回答

2

这里的问题是,您没有0123'的所有者ProductGroup,所以它不知道你指的是什么。您需要创建一个类型为ProductGroup的新属性,该属性也可以称为ProductGroup。

public ProductGroup ProductGroup {get; set;} 

这是从#3开始使用c# auto-implemented properties。尽管如此,您也可以使用后台字段来完成此操作。

然后你会改变你的构造函数保存这个变量(您已经在传递):

public Product(string name, ProductGroup productGroup) 
{ 
    this.Name = name; 
    this.ProductGroup = productGroup; 
} 

请注意,我已经改变了参数的构造函数的类型以及添加的分配。很明显,以同样的方式更新你的其他构造函数,然后它应该像你希望的那样工作。

+0

这样做了,现在我得到了'不能将类型'object'隐式转换为'Basics.ProductGroup'。存在明确的转换(您是否缺少一个转换?)' – Tatu 2014-09-03 12:48:20

+0

您是否已将参数的类型更改为构造函数?你不应该在你的代码中任何地方都有'object'(你几乎总是想要像我在更新的构造函数中那样指定一个更具体的类型)。 – Chris 2014-09-03 13:09:03

0

除了克里斯的回答,我认为这可能有助于赋予该属性与其类型不同的名称。开始时它可以防止可能的混淆。例如:

public Product(string name, ProductGroup group) 
{ 
    this.Name = name; 
    this.Group = group; 
} 

public ProductGroup Group {get; set;} 
+0

进一步询问,如何将文字写入字段组?使用'string group'''返回给我'不能隐式地将类型'字符串'转换为'Basics.ProductGroup'' – Tatu 2014-09-03 11:55:31

+0

group.Name =“text”; – 2014-09-03 14:13:04

相关问题