[英]c# class variable type another class
i have a two class. 我有两个班。 Product and Color.
产品和颜色。 how to access ProductColor(name,id) sample :
如何访问ProductColor(name,id)示例:
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
本身设置为新实例:
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: 从那里,您可以在Product中调用要在Color中使用的属性:
p.SomeColor = some variable you set in the Color class;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.