我经常用来with创建一个对象并运行它的方法。它使代码看起来干净:
With New MyObj(...)
.Prop1 = Val1
.Prop2 = Val2
.Run()
End With
Run Code Online (Sandbox Code Playgroud)
但是,有时我想返回该对象:
With New MyObj(...)
.Prop1 = Val1
.Prop2 = Val2
Return .Me
End With
Run Code Online (Sandbox Code Playgroud)
但并非所有对象都有 Me (this) 属性,那么我如何在 中引用相关对象呢with?
好吧,我想答案是,只要我可以更改相关对象的定义,我就可以这样做:
Public Class XC
Public Self As XC = Me
End Class
With New XC()
Dim x As XC = .Self
End With
Run Code Online (Sandbox Code Playgroud)