在VB.Net中构造对象并调用没有分配的方法 [英] Constructing an object and calling a method without assignment in VB.Net
本文介绍了在VB.Net中构造对象并调用没有分配的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不完全知道C#做什么用,所以我没有运气来寻找VB.Net等效语法(如果存在,我怀疑可能不行)。
I'm not entirely sure what to call what C# does, so I haven't had any luck searching for the VB.Net equivalent syntax (if it exists, which I suspect it probably doesn't).
在c#中,您可以执行以下操作:
In c#, you can do this:
public void DoSomething() {
new MyHelper().DoIt(); // works just fine
}
但是据我所知,在VB中。净,您必须将帮助对象分配给局部变量,否则您只会遇到语法错误:
But as far as I can tell, in VB.Net, you must assign the helper object to a local variable or you just get a syntax error:
Public Sub DoSomething()
New MyHelper().DoIt() ' won't compile
End Sub
在混合语言项目中,我每天遇到的好奇心只是其中之一-经常有一个VB.Net等效项,它使用的语法不太明显。有人吗?
Just one of those curiosity things I run into from day to day working on mixed language projects - often there is a VB.Net equivalent which uses less than obvious syntax. Anyone?
推荐答案
神奇的词是Call。
Public Sub DoSomething()
Call (New MyHelper()).DoIt()
Call New MyHelper().DoIt()
End Sub
这篇关于在VB.Net中构造对象并调用没有分配的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文