|
■No86055 (ポスト さん) に返信 > 現在、Listクラスにてデータを入れ子にし、3階層のリストを作成したいのですが上手くいきません。 > よければ助言を頂けないでしょうか。
作業用の変数を一つ用意すると楽かな。
Dim postData As New Person() Dim child As ChildId_1
'親に子を追加してから、その子に孫を追加していく child = New ChildId_1() With {.c = "子1"} postData.a.Add(child) child.b.Add(New ChildId_2() With {.d = "子1-孫1"}) child.b.Add(New ChildId_2() With {.d = "子1-孫2"}) child.b.Add(New ChildId_2() With {.d = "子1-孫3"}) child = New ChildId_1() With {.c = "子2"} postData.a.Add(child) child.b.Add(New ChildId_2() With {.d = "子2-孫1"}) child.b.Add(New ChildId_2() With {.d = "子2-孫2"}) child.b.Add(New ChildId_2() With {.d = "子2-孫3"}) child = New ChildId_1() With {.c = "子3"} postData.a.Add(child) child.b.Add(New ChildId_2() With {.d = "子3-孫1"}) child.b.Add(New ChildId_2() With {.d = "子3-孫2"}) child.b.Add(New ChildId_2() With {.d = "子3-孫3"})
あるいはこう。
Dim postData As New Person() Dim child As ChildId_1
'子に孫を追加してから、その子を親に追加していく child = New ChildId_1() With {.c = "子1"} child.b.Add(New ChildId_2() With {.d = "子1-孫1"}) child.b.Add(New ChildId_2() With {.d = "子1-孫2"}) child.b.Add(New ChildId_2() With {.d = "子1-孫3"}) postData.a.Add(child) child = New ChildId_1() With {.c = "子2"} child.b.Add(New ChildId_2() With {.d = "子2-孫1"}) child.b.Add(New ChildId_2() With {.d = "子2-孫2"}) child.b.Add(New ChildId_2() With {.d = "子2-孫3"}) postData.a.Add(child) child = New ChildId_1() With {.c = "子3"} child.b.Add(New ChildId_2() With {.d = "子3-孫1"}) child.b.Add(New ChildId_2() With {.d = "子3-孫2"}) child.b.Add(New ChildId_2() With {.d = "子3-孫3"}) postData.a.Add(child)
child 変数を作らずにまとめて書く場合はこう。
Dim postData As New Person() With { .a = New List(Of ChildId_1)() From { New ChildId_1() With { .c = "子1", .b = New List(Of ChildId_2) From { New ChildId_2() With {.d = "子1-孫1"}, New ChildId_2() With {.d = "子1-孫2"}, New ChildId_2() With {.d = "子1-孫3"} } }, New ChildId_1() With { .c = "子2", .b = New List(Of ChildId_2) From { New ChildId_2() With {.d = "子2-孫1"}, New ChildId_2() With {.d = "子2-孫2"}, New ChildId_2() With {.d = "子2-孫3"} } }, New ChildId_1() With { .c = "子3", .b = New List(Of ChildId_2) From { New ChildId_2() With {.d = "子3-孫1"}, New ChildId_2() With {.d = "子3-孫2"}, New ChildId_2() With {.d = "子3-孫3"} } } } }
|