[英]Serializing derived class from BindingList and ISerializable interface
[英]Accessing BindingList from another class
我正在嘗試從另一個類訪問我的BindingList,但不確定如何執行。
這是我的Form2類中的代碼:
public partial class Form2 : Form
{
//List containing the memos added
public static BindingList<memo> memosList = new BindingList<memo>();
//Now the list needs to be initialized so i can use it elsewhere
public static BindingList<memo> Memos
{
get { return memosList; }
set { memosList = value; }
}
//...
這是我的Form3類中的代碼:
private void CreateMemo(object sender, EventArgs args)
{
memo memosList = new memo();
memosList.Add(
new memo(priorityNumber.Text, memoTitle.Text, memoDescription.Text));
}
我將如何訪問Form2中的列表,以便可以從中添加/刪除項目?
BindingList<memo>
被聲明為靜態屬性,因此您可以通過使用Form2.Memos
在Form3
類中添加或刪除項來訪問它。
private void CreateMemo(object sender, EventArgs args)
{
Form2.Memos.Add(
new memo(priorityNumber.Text, memoTitle.Text, memoDescription.Text));
}
因為您已將備忘錄列表設為公共靜態,所以為其擁有屬性是沒有意義的。 Martijn van Put提供了一個很好的答案,但是我不明白為什么要有兩個指向相同內存位置的變量名。
這也和Martijn van Put一樣
private void CreateMemo(object sender, EventArgs args)
{
Form2.memoslist.Add(
new memo(priorityNumber.Text, memoTitle.Text, memoDescription.Text));
}
這是使用屬性的參考... https://msdn.microsoft.com/zh-cn/library/w86s7x04.aspx
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.