我試了兩天谷歌搜索,但似乎無法找到答案。何處/何時填充查找值?
我想讓Category類根據輸入的id提供描述,並在id無效時返回一個錯誤。這是最好的方法嗎?
public class Category
{
private int _id;
private string _desc;
public Category(int id)
{
ID = id;
}
public int ID
{
get
{
return _id;
}
set
{
_id = value;
//_desc = value from data access layer or throw error if the ID is invalid
}
}
public string Description
{
get
{
return _desc;
}
}
}
public class Person
{
public int ID {get; set;}
public Category Category {get; set;}
}
public class MyApp
{
static void Main()
{
Person p = new Person();
Category c = new Category(2);
p.Category = c;
}
}
你會從哪裏獲得這些查找值?他們是硬編碼的嗎? –
不,他們將被存儲在數據庫中 – Tom