나는 창조물을 어떻게 얻을 수 있는지 알고 싶다. Product라는 클래스와 Static 클래스 인 Inventory라는 두 클래스가 있습니다. 입력 한 제품의 객체가 생성이되면,개체를 만들 때 어떻게 가져올 수 있습니까?
public static class Inventory
{
public static List<Product> Products { get; set; }
}
것은 내가이 다음의하고 싶으면 무엇 : 여기에 재고 클래스의 정의입니다
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public int Quantity { get; set; }
}
: 다음은 제품 클래스의 정의입니다 이 개체는 Inventory.Products
목록에 추가되어야하므로 나중에 인벤토리 정보를 쿼리 할 수 있습니다.
나는 제품 클래스 내에서 같은 것을 할 수 있도록하고 싶습니다 :
public class Product
{
public Product()
{
Inventory.Products.Add(this);
}
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public int Quantity { get; set; }
}
을 그러나 나는 내가 NullReferenceException
얻을 것이다이 작업을 수행 할 생성자를 사용하는 경우, Products
가 아직 초기화되지 않기 때문에. 정적 인 Products
목록을 초기화하는 가장 좋은 곳은 어디입니까?
여기서'Inventory.Products = new List(); '을 (를) 호출 하시겠습니까? 그것이 NullRefeferenceException의 이유가 아니었다면, 당신은'Products'를 초기화하지 않았습니다. –
이 물체에서 무엇을 얻고 있습니까? 당신의 디버그가 제안하는 것 ?? –
@ RenéVogt 감사합니다. 문제는 그것이 사실입니다. 그러나 인벤토리가 정적 클래스이고 생성자를 가질 수없는 경우 Products 속성을 초기화하는 올바른 방법은 어디에 있습니까? – artie