0

I would like to know how I can get an object upon it's creation. I have two classes, a class called Product, and another class called Inventory, which is a static class. Here's the definition of the product class:

public class Product
{       
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
    public int Quantity { get; set; }
}

Here's the definition of the Inventory class:

public static class Inventory
{
    public static List<Product> Products { get; set; }   
}

What I would like to do it's the following, when an object of type Product it's created, this object should be added to the Inventory.Products list, so later I can query for inventory information.

I would like to be able to do something like this inside the Product class:

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; }
}

However if I use the constructor to do this I will get an NullReferenceException, because Products is not yet initialized. Where is the best place to initialize the static Products list?

René Vogt
  • 43,056
  • 14
  • 77
  • 99
adamasan
  • 1,092
  • 1
  • 12
  • 33
  • Where do you call `Inventory.Products = new List();`? If nowhere, that's the reason for the NullRefeferenceException, you did not initialize `Products`. – René Vogt Dec 29 '16 at 13:19
  • What you're getting in this object. What your debug suggests?? – Dirty Developer Dec 29 '16 at 13:20
  • @RenéVogt Thank you. The problem it's indeed that. But where would be the correct way to initialize the Products property if Inventory it's an static class and can't have constructors? – adamasan Dec 29 '16 at 13:34
  • @DirtyDeveloper In the object I get all of it's properties. However I don't know where to initialize the list, since Inventory it's a static class. – adamasan Dec 29 '16 at 13:37
  • In c#6 you could change the declaration to `public static List Products { get; } = new List();` creating and read-only property. In older c# versions you could create a readonly backing field and initialize it and let the getter only return that backing field. – René Vogt Dec 29 '16 at 13:38

2 Answers2

1

The null reference isn't the Product, it's the list. Simply initialize your static list before trying to use it. Its own class' static initializer would be a good place to do that:

public static class Inventory
{
    public static List<Product> Products { get; set; }   

    static Inventory()
    {
        Products = new List<Product>();
    }
}
David
  • 208,112
  • 36
  • 198
  • 279
  • Agree! But not sure what he wants? Is it the correct OOP way to handle? – Dirty Developer Dec 29 '16 at 13:22
  • David, thank you, the problem it's indeed that. However Inventory it's a static class and therefore as far as I know can't have constructors. Where would be the best place to initialize the list? – adamasan Dec 29 '16 at 13:36
  • @arthur: In the *static* constructor, as demonstrated in the answer. Did you encounter an error when you attempted this? – David Dec 29 '16 at 13:39
  • @David You're right. It worked after I rebuild the whole solution.Thank you very much. – adamasan Dec 29 '16 at 13:43
  • Using static list is not quite thread safe, should consider some better alternatives. – Janne Matikainen Dec 29 '16 at 13:50
1

There are different possibilities to initialize a static property. You can for example use a readonly backing field:

public static class Inventory
{
    private static readonly List<Product> products = new List<Product>();
    public static List<Product> Products { get { return products; } }
}

Since C# 6 you can use a readonly property instead:

public static class Inventory
{        
    public static List<Product> Products { get; } = new List<Product>();
}

Or (as David already suggested) you can use a static constructor:

public static class Inventory
{        
    public static List<Product> Products { get; private set; }
    static Inventory()
    {
         Products = new List<Product>();
    }
}
Community
  • 1
  • 1
René Vogt
  • 43,056
  • 14
  • 77
  • 99