Saturday, 15 March 2014

c# - Property Class having class object as a Property -



c# - Property Class having class object as a Property -

i having proeprty class propuser:

public class propuser { public int userid { get; set; } public int parentid { get; set; } public propproduct product { get; set; } }

here, 3rd property product object of propproduct property class

public class propproduct { public int productid { get; set; } public string name { get; set; } public int cost { get; set; } }

now, in below code:

propuser user = new propuser(); user.product.name = "reebok";

user.product throwing exception of "object reference not set instance of object."

i know user.product null, how initialize can set user.product.name="reebok"

if want initialized class created, create constructor , initialize there:

public class propuser { public propuser { product = new propproduct(); } public int userid { get; set; } public int parentid { get; set; } public propproduct product { get; set; } }

c# .net

No comments:

Post a Comment