Harsha W
Harsha W

Reputation: 3376

Initialize Nested List<T>

Please refer the below code

List<ProductDM> productDMList = _orderRepo.GetProductList(32, 12);
  for (int i=0;i<productDMList.Count;i++)
        {
            productDMList[i].CabinetList[i].Min = productDMList[i].Min;
            productDMList[i].CabinetList[i].Max = productDMList[i].Max;
        }

public class ProductDM
  {
    public List<InventoryDM> CabinetList { get; set; }
    public double Min { get; set; }
    public double Max { get; set; }
 }
public class InventoryDM
 {
    public Double Min { get; set; }
    public Double Max { get; set; }
 }

The requirement is to loop through the productDMList and bind the returned MIN and MAX values inside the cabinet list. ProductDM fills with MIN and MAX amounts but when assigning those to CabinetList, it returns an error.

This is because the CabinetList is initially empty and it doesn't show the MIN MAX properties in it.

Issue

I am using the above code to assign data but returns

Object reference not set to an instance of an object.

because of the CabinetList empty.

How to initialize the cabinet list here???

Upvotes: 0

Views: 1598

Answers (3)

Marco Forberg
Marco Forberg

Reputation: 2644

Depending on your requirements you could do something like this

public class ProductDM
{
  public List<InventoryDM> CabinetList { get; private set; }
  public double Min { get; set; }
  public double Max { get; set; }

  public ProductDM()
  {
    CabinetList = new List<InventoryDM>();
  }
}

or if you get your CabinetList data from an external source, e.g. database:

public class ProductDM
{
  private List<InventoryDM> _cabinetList = null;

  public double Min { get; set; }
  public double Max { get; set; }

  public List<InventoryDM> CabinetList
  { get
    {
      if(_cabinetList == null)
      {
        _cabinetList = ... // retrieve data from external source
      }
      return _cabinetList; 
    }
  }
}

Upvotes: 2

croxy
croxy

Reputation: 4178

As Marco Forberg's answer states initializing the CabinetList in the constructor prevents the

Object reference not set to an instance of an object.

exception.

In addition to this, instead of assigning the Min and Max values via the index accessor:

productDMList[i].CabinetList[i].Min = productDMList[i].Min;
productDMList[i].CabinetList[i].Max = productDMList[i].Max;

You should use the Add() method of the List<> type:

productDMList[i].CabinetList.Add(new InventoryDM { Min = productDMList[i].Min, Max = productDMList[i].Max });

otherwise you will get an

ArgumentOutOfRangeException

because you try to access an item on the list which doesn't exist yet.

Upvotes: 2

Giovanni Patruno
Giovanni Patruno

Reputation: 773

The issue is not the emptiness of the CabinetList, instead the list of objects is null as you can see in the debugger.
In order to initialize the list you can refer to the answers of this post: How to initialize a C# string list (List<string>) with many string values

Note that you don't have a list of string but a list of <InventoryBM> objects, but the concept is analogous.

Upvotes: 1

Related Questions