-2

I am getting productTestInformation as null at set.

 public class ProductLog
    {
        public ProductTestInformation productTestInformation { get; set; }

        public int? ProductId { get { return productTestInformation .ProductId; } set { productTestInformation .ProductId = value; } }
        public string ProductName { get { return productTestInformation .ProductName; } set { productTestInformation .ProductName = value; } }

    }
Zein Makki
  • 29,485
  • 6
  • 52
  • 63
Bhavsar Jay
  • 163
  • 1
  • 6

4 Answers4

2

You didn't initialize your object. Like this in C# 6 :

public ProductTestInformation productTestInformation { get; set; } 
                                                           = new ProductTestInformation();

The default value for a Reference Type is NULL. Since you didn't initialize your object (using the new keyword). Then it is referring to NULL

Zein Makki
  • 29,485
  • 6
  • 52
  • 63
1

productTestInformation will always be null until you set from it.

If you would like it to return a new object try something like this:

private ProductTestInformation _productTestInformation;
public ProductTestInformation productTestInformation { 
    get { return _productTestInformation ?? (_productTestInformation = new ProductTestInformation()); }
    set {_productTestInformation = value;} 
}
David Haxton
  • 283
  • 1
  • 10
1

Initilize "productTestInformation" in Constructor

public class ProductLog
{
    public ProductTestInformation productTestInformation { get; set; }

    public int? ProductId { get { return productTestInformation .ProductId; } set { productTestInformation .ProductId = value; } }
    public string ProductName { get { return productTestInformation .ProductName; } set { productTestInformation .ProductName = value; } }

    public ProductLog()
    {
        this.productTestInformation = new ProductTestInformation();
    }
}
0

I think this will help you.

public class ProductLog

{
    private ProductTestInformation _productTestInformation;

    public ProductLog(ProductTestInformation productTestInformation)
    {
        _productTestInformation = productTestInformation;
    }

    public int? ProductId { get { return _productTestInformation.ProductId; } set { _productTestInformation.ProductId = value; } }
    public string ProductName { get { return _productTestInformation.ProductName; } set { _productTestInformation.ProductName = value; } }

}

public class ProductTestInformation 
{
    public int ProductId { get; set; }
    public string ProductName { get; set; }
}

public class ProductController : Controller
{
    public ActionResult View() 
    {
        var productTestInfo = new ProductTestInformation();// or get from the db
        var productlog = new ProductLog(productTestInfo);

        return View();
    }
}
mohi
  • 64
  • 3