简体   繁体   中英

c# class variable type another class

i have a two class. Product and Color. how to access ProductColor(name,id) sample :

public class tblColor
{
    public int Id { get; set; }
    public string ColorName { get; set; }
}

 public class Urun
{
    public int ProductId { get; set; }
    public string ProductName { get; set; }
    public tblRenk ProductColor { get; set; }
}

while (dr.Read())
{
    products.Add(new Product()
    {
        ProductId = Convert.ToInt32(dr["id"].ToString()),
        ProductName = dr["ProdName"].ToString(),
        ?????? ProductColor  = dr["ColName"].ToString() 
    });
}

You need to set ProductColor itself to a new instance:

ProductColor = new Color()

You will probably want to initialize its properties with a nested { ... } block.

You need to instantiate the class with the property in it and then call it. in your case, you would do:

Product p = new Product();

From there you could call the property in Product to be used in Color:

p.SomeColor = some variable you set in the Color class;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM