ListBoxのItemsプロパティを調べるとListBox.ObjectCollectionがどうのこうのと説明されてもピンとこなかった。
だいたいプロパティにインデックスがある時点で何故に?となるでしょ。配列?インデクサ?何じゃコレ?みたいな。
リファレンスってどうしてこう回りくどくて分かりづらく書いてるのかねぇ。たまに「説明になってないぞよ」とツッコミを入れる。
だが私も少々進歩して、結局のところこういうことでしょ?みたいなモノを書いてみた。合ってるかどうかは責任持ちませぬ。
class Program
{
static void Main(string[] args)
{
ParentObject po = new ParentObject();
po.Items.Add("foo");
po.Items.Add(123);
Console.WriteLine(
"Items[0] = {0}\nItems[1] = {1}",
po.Items[0], po.Items[1]);
Console.ReadLine();
}
}
class ItemCollection
{
List<object> _items;
public ItemCollection()
{
_items = new List<object>();
}
public object this[int index]
{
get
{
return _items[index];
}
}
public void Add(object item)
{
_items.Add(item);
}
}
class ParentObject
{
ItemCollection _itemCollection;
public ParentObject()
{
_itemCollection = new ItemCollection();
}
public ItemCollection Items
{
get
{
return _itemCollection;
}
}
}
ParentObjectをListBox、ItemCollectionをObjectCollectionに置き換えたら分かるかな?ListBox.SelectedIndicesプロパティやListBox.SelectedItemsプロパティも内部で値を保持するListがいつ更新されるかは別として、こんな感じでできてるんではなかろ~か。





0 Comments:
コメントを投稿