问题
I am wondering if this is possible. I have a List Table (lstTable) that is on the same form that I am trying to fill in with information from a public structure (ELEM_DATA). I understand nested with statements will work if it is within the same scope but how can I do this with example 2 below:
Example 1:
With me.lstTable.Items(RECORD)
.SubItems(1).text = ELEM_DATA(RECORD).name
.SubItems(2).text = ELEM_DATA(RECORD).number
end with
Example 2:
With me.lstTable.Items(RECORD)
With ELEM_DATA(RECORD)
.SubItems(1).text = .name
.SubItems(2).text = .number
end with
end with
I didnt know if it is possible or if it would be as simple as changing (.name) to something else.
回答1:
Nested With statements work (see comment about conflicts). Unfortunately you can't use the outer members inside the inner with. But since your outer WITH is a refernce type you could use a local variable to "alias" it as you suggest in you comment.
Dim l = me.lstTable.Items(RECORD) ' requires 2008 and option infer
With ELEM_DATA(RECORD)
l.SubItems(1).text = .name
End With
Here's a link to show how nested WITH statements can used.
http://ideone.com/agjne
回答2:
'EXAMPLE:
With New Object() {1, 2} 'With block 1
Dim debug_1 As Object = .ToArray(0) 'Value 1 from object of block 1
With New Object() {3, 4} 'With block 2
Dim debug_2 As Object = .ToArray(0) 'Value 3 from block 2
'How get 2 from block 1 of Object() {1, 2}?
'>>>
End With
End With
'SOLUTION
With New Object() {1, 2}
With New Object() {3, 4}.Concat(.ToArray)
Dim debug_3 As Object = .ToArray(3) 'got value 2 from outer WITH block 1 [Object() {1, 2}]
'---//---//---
End With
'---//---//---
End With
来源:https://stackoverflow.com/questions/6656725/vb-net-nested-with-statements-from-different-scopes