我有兩個在運行時實例化並加載的類。我想檢查一個資源,而不必引用這些實例,因爲如果有很多檢查和類,它可能會變得混亂。跨實體通信而無需引用實例C#
例如,如果我有以下兩個類,並且我想調用另一個類。
Class Item
{
private int id;
private int loc;
void Item()
{
// the Class actually has some properties with get set, but thats not the point here.
id = 1;
loc = 2;
}
public bool check()
{
// Check if the several fields are ok for DB submission
// how Can I refer to the other class from here? Do I have to pass the instance as a parameter?
return Locals.Exists(loc); // does not work because its not static!
}
}
Class Locals
{
Hashtable l = new Hastable();
void Locals()
{
// This will actually be loaded from a DB at runtime.
l.add(1, "Local 1");
l.add(2, "Local 2");
}
public bool Exists(int i)
{
return l.ContainsKey(i);
}
}
//Form Code:
main()
{
Item newItem = new Item();
Locals allLocals = new Locals();
newItem.check();
}
有沒有辦法做到這一點,而不必調用
newItem.check(allLocals);
從我所看到的,即使有代表,主叫CLASSE需要其他類的實例。
總之,是否有另一種方式來促進跨實例通信?
我很困惑嗎?
你可以簡單地使用某種形式的聚合器或消息發佈系統,但是這種有點像代碼味道......你爲什麼需要這樣做? – Firoso 2010-07-07 16:41:03
我會檢查數據庫,而不是實例化的類。 – Fosco 2010-07-07 16:42:35
如果您不指定要檢查哪些當地人,您怎麼知道要檢查哪一個(甚至哪個)? – 2010-07-07 16:43:10