VB.NET 不同范围的嵌套 With 语句

发布于 2024-11-19 10:10:37 字数 529 浏览 3 评论 0原文

我想知道这是否可能。我有一个列表表(lstTable),它与我试图用公共结构(ELEM_DATA)中的信息填写的表单相同。我知道如果嵌套 with 语句在同一范围内,则它会起作用,但是我如何使用下面的示例 2 来执行此操作:

示例 1:

With me.lstTable.Items(RECORD)
     .SubItems(1).text = ELEM_DATA(RECORD).name
     .SubItems(2).text = ELEM_DATA(RECORD).number
end with

示例 2:

With me.lstTable.Items(RECORD)
     With ELEM_DATA(RECORD)
     .SubItems(1).text = .name
     .SubItems(2).text = .number
     end with
end with

我不知道是否可能,或者是否像更改 (.name 一样简单) )到别的东西。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苏别ゝ 2024-11-26 10:10:37

嵌套 With 语句有效(请参阅有关冲突的评论)。不幸的是,您不能在内部成员中使用外部成员。但是由于您的外部WITH是引用类型,您可以使用局部变量来“别名”它,正如您在评论中所建议的那样。

Dim l = me.lstTable.Items(RECORD) ' requires 2008 and option infer
With ELEM_DATA(RECORD)
   l.SubItems(1).text = .name
End With

下面的链接展示了如何使用嵌套的WITH语句。

http://ideone.com/agjne

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

最后的乘客 2024-11-26 10:10:37
'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
'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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文