2016-11-17 102 views
0

我也可以在這裏顯示這個所謂的「類別」對象屬性:對象屬性導致錯誤

private List<Category.Categories> _categories; 
     public List<Category.Categories> Categories 
     { 
      get { return _categories; } 
      set 
      { 
       if (!Enum.IsDefined(typeof(Category.Categories), value)) 
       { 
        _categories = null; 
       } 

       _categories = value; 
      } 
     } 

我嘗試使用這個屬性返回我的對象​​,但我得到的是一個'System.NullReferenceException:對象未將引用設置爲對象的實例。錯誤。如果我從instantiaziation返回中刪除這個屬性,那麼我沒有問題。我不知道爲什麼這會導致錯誤。該物業只是想從這個類接受枚舉:

public class Category 
    { 
     public enum Categories 
     { 
      Footwear, 
      Electronics, 
      Jewellery, 
      Restaurants, 
      Services, 
      Apparel 
     } 
    } 

最後,這是我如何設置屬性:

Categories = { Categories.Apparel } 

回答

0

你必須初始化列表存儲列表形成集合財產

private List<Category.Categories> _categories = new List<Category.Categories>(); 
+0

天才謝謝你 – JayPhillips

相關問題