如何在With ... End With中访问对象本身

时间:2021-09-06 16:59:56

Some code to illustrate my question:

一些代码来说明我的问题:

With Test.AnObject

    .Something = 1337
    .AnotherThing = "Hello"

    ''// why can't I do this to pass the object itself:
    Test2.Subroutine(.)
    ''// ... and is there an equivalent, other than repeating the object in With?

End With

4 个解决方案

#1


There is no way to refer to the object referenced in the With statement, other than repeating the name of the object itself.

除了重复对象本身的名称之外,无法引用With语句中引用的对象。

EDIT

If you really want to, you could modify your AnObject to return a reference to itself

如果您真的想要,可以修改AnObject以返回对自身的引用

Public Function Self() as TypeOfAnObject
    Return Me
End Get

Then you could use the following code

然后你可以使用以下代码

With Test.AnObject
    Test2.Subroutine(.Self())
End With

Finally, if you cannot modify the code for AnObject, you could (but not necessarily should) accomplish the same thing via an extension method. One generic solution is:

最后,如果您无法修改AnObject的代码,您可以(但不一定应该)通过扩展方法完成相同的操作。一个通用解决方案是:

' Define in a Module
<Extension()>
Public Function Self(Of T)(target As T) As T
    Return target
End Function

called like so:

如此称呼:

Test2.Subroutine(.Self())

or

With 1
   a = .Self() + 2 ' a now equals 3
End With

#2


I suspect you'll have to repeat yourself. If the expression (to get the object) is expensive, then perhaps drop it into a variable first, and either use that variable in the With, or drop the With completely:

我怀疑你不得不重复自己。如果表达式(获取对象)很昂贵,那么可能先将其放入变量中,然后在With中使用该变量,或者完全删除With:

tmp = Test.AnObject;
tmp.Something = 1337;
...
Test2.Subroutine(tmp);

#3


As others have said, you're going to have to write

正如其他人所说,你将不得不写作

Test2.Subroutine(Test.AnObject)

This is a good example of why it's worth being a little careful with the With construct in VB.Net. My view is that to make it worth using at all, you really need to be setting more than one or two properties, and/or calling more than one or two methods on the object in the With statement.

这是为什么值得对VB.Net中的With构造稍微小心的一个很好的例子。我的观点是,为了使它值得使用,你真的需要设置多个或两个属性,和/或在With语句中调用对象上的一个或两个以上的方法。

When there are lots, and you're not interspersing the .SomeProperty = , or .DoSomething, with other things, it's a terrific aid to readability.

当有很多东西,并且你没有将.SomeProperty =或.DoSomething与其他东西一起穿插时,它对可读性有很大的帮助。

Conversely, a few dots sprinkled amongst a load of other stuff is actually a lot harder to read than not using With at all.

相反,在其他东西中散布的几个点实际上比不使用With更难阅读。

In this case, . characters on their own could easily get lost visually, although of course, it would be syntactically consistent.

在这种情况下, 。他们自己的角色很容易在视觉上迷失,虽然当然,它在语法上是一致的。

I guess they just chose not to implement it. VB isn't really the sort of language where they want to encourage single character language elements, and as a heavy user of VB.Net, I broadly agree with that.

我猜他们只是选择不实施它。 VB并不是他们想要鼓励单字符语言元素的那种语言,作为VB.Net的重度用户,我大致同意这一点。

Bottom line: if you're using a With clause with many contained elements, having to refer to the object itself isn't that big a deal. If you're using it with just one or two, maybe better not to use a With clause in the first place.

结论:如果你使用带有许多包含元素的With子句,那么必须引用对象本身并不是什么大不了的事。如果你只使用一两个,最好不要首先使用With子句。

#4


I'm not sure this is an "answer", per se, but it does illustrate another reason to want a short-hand reference to the parent in a With.

我不确定这本身就是一个“答案”,但它确实说明了想要在With中对父级进行简短引用的另一个原因。

Here's a code sample using a "bare With" (that's what I call it, anyway):

这是使用“bare With”的代码示例(无论如何,这就是我所说的):

With New frmMySubForm
    .lblLinkLabel.Links.Add(New LinkLabel.Link With {.Name = "link", .LinkData = "someUrl", .Start = .lblLinkLabel.Text.IndexOf("link"), .Length = "link".Length})
    ...
End With

But you actually can't code that because in the term .Start = .lblLinkLabel.Text.IndexOf("link") the compiler expects anything starting with . to be a member of LinkLabel.Link, which .lblLinkLabel isn't.

但实际上你无法对其进行编码,因为在术语.Start = .lblLinkLabel.Text.IndexOf(“link”)中,编译器期望任何以。成为LinkLabel.Link的成员,.lblLinkLabel不是。

What would be good, I think, is to be able to write something like:

我认为,最好的是能够写出如下内容:

With New frmMySubForm
    .lblLinkLabel.Links.Add(New LinkLabel.Link With {.Name = "link", .LinkData = "someUrl", .Start = Me.lblLinkLabel.Text.IndexOf("link"), .Length = "link".Length})
    ...
End With

where Me in this scope is taken to be New frmMySubForm.

此范围内的Me被视为新的frmMySubForm。

Yes, I realize that I'm being picky and I could easily assign a variable, etc. But the example form is something I use a lot simply out of preference.

是的,我意识到我很挑剔,我可以很容易地分配一个变量,等等。但是示例表单是我使用很多只是偏好的东西。

#1


There is no way to refer to the object referenced in the With statement, other than repeating the name of the object itself.

除了重复对象本身的名称之外,无法引用With语句中引用的对象。

EDIT

If you really want to, you could modify your AnObject to return a reference to itself

如果您真的想要,可以修改AnObject以返回对自身的引用

Public Function Self() as TypeOfAnObject
    Return Me
End Get

Then you could use the following code

然后你可以使用以下代码

With Test.AnObject
    Test2.Subroutine(.Self())
End With

Finally, if you cannot modify the code for AnObject, you could (but not necessarily should) accomplish the same thing via an extension method. One generic solution is:

最后,如果您无法修改AnObject的代码,您可以(但不一定应该)通过扩展方法完成相同的操作。一个通用解决方案是:

' Define in a Module
<Extension()>
Public Function Self(Of T)(target As T) As T
    Return target
End Function

called like so:

如此称呼:

Test2.Subroutine(.Self())

or

With 1
   a = .Self() + 2 ' a now equals 3
End With

#2


I suspect you'll have to repeat yourself. If the expression (to get the object) is expensive, then perhaps drop it into a variable first, and either use that variable in the With, or drop the With completely:

我怀疑你不得不重复自己。如果表达式(获取对象)很昂贵,那么可能先将其放入变量中,然后在With中使用该变量,或者完全删除With:

tmp = Test.AnObject;
tmp.Something = 1337;
...
Test2.Subroutine(tmp);

#3


As others have said, you're going to have to write

正如其他人所说,你将不得不写作

Test2.Subroutine(Test.AnObject)

This is a good example of why it's worth being a little careful with the With construct in VB.Net. My view is that to make it worth using at all, you really need to be setting more than one or two properties, and/or calling more than one or two methods on the object in the With statement.

这是为什么值得对VB.Net中的With构造稍微小心的一个很好的例子。我的观点是,为了使它值得使用,你真的需要设置多个或两个属性,和/或在With语句中调用对象上的一个或两个以上的方法。

When there are lots, and you're not interspersing the .SomeProperty = , or .DoSomething, with other things, it's a terrific aid to readability.

当有很多东西,并且你没有将.SomeProperty =或.DoSomething与其他东西一起穿插时,它对可读性有很大的帮助。

Conversely, a few dots sprinkled amongst a load of other stuff is actually a lot harder to read than not using With at all.

相反,在其他东西中散布的几个点实际上比不使用With更难阅读。

In this case, . characters on their own could easily get lost visually, although of course, it would be syntactically consistent.

在这种情况下, 。他们自己的角色很容易在视觉上迷失,虽然当然,它在语法上是一致的。

I guess they just chose not to implement it. VB isn't really the sort of language where they want to encourage single character language elements, and as a heavy user of VB.Net, I broadly agree with that.

我猜他们只是选择不实施它。 VB并不是他们想要鼓励单字符语言元素的那种语言,作为VB.Net的重度用户,我大致同意这一点。

Bottom line: if you're using a With clause with many contained elements, having to refer to the object itself isn't that big a deal. If you're using it with just one or two, maybe better not to use a With clause in the first place.

结论:如果你使用带有许多包含元素的With子句,那么必须引用对象本身并不是什么大不了的事。如果你只使用一两个,最好不要首先使用With子句。

#4


I'm not sure this is an "answer", per se, but it does illustrate another reason to want a short-hand reference to the parent in a With.

我不确定这本身就是一个“答案”,但它确实说明了想要在With中对父级进行简短引用的另一个原因。

Here's a code sample using a "bare With" (that's what I call it, anyway):

这是使用“bare With”的代码示例(无论如何,这就是我所说的):

With New frmMySubForm
    .lblLinkLabel.Links.Add(New LinkLabel.Link With {.Name = "link", .LinkData = "someUrl", .Start = .lblLinkLabel.Text.IndexOf("link"), .Length = "link".Length})
    ...
End With

But you actually can't code that because in the term .Start = .lblLinkLabel.Text.IndexOf("link") the compiler expects anything starting with . to be a member of LinkLabel.Link, which .lblLinkLabel isn't.

但实际上你无法对其进行编码,因为在术语.Start = .lblLinkLabel.Text.IndexOf(“link”)中,编译器期望任何以。成为LinkLabel.Link的成员,.lblLinkLabel不是。

What would be good, I think, is to be able to write something like:

我认为,最好的是能够写出如下内容:

With New frmMySubForm
    .lblLinkLabel.Links.Add(New LinkLabel.Link With {.Name = "link", .LinkData = "someUrl", .Start = Me.lblLinkLabel.Text.IndexOf("link"), .Length = "link".Length})
    ...
End With

where Me in this scope is taken to be New frmMySubForm.

此范围内的Me被视为新的frmMySubForm。

Yes, I realize that I'm being picky and I could easily assign a variable, etc. But the example form is something I use a lot simply out of preference.

是的,我意识到我很挑剔,我可以很容易地分配一个变量,等等。但是示例表单是我使用很多只是偏好的东西。