Ata
Ata

Reputation: 11

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

Upvotes: 0

Views: 111

Answers (2)

Brian
Brian

Reputation: 5119

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;

Upvotes: 1

SLaks
SLaks

Reputation: 888047

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.

Upvotes: 3

Related Questions