Constructing an object and calling a method without assignment in VB.Net

Posted by mdryden on Stack Overflow See other posts from Stack Overflow or by mdryden
Published on 2010-04-15T20:21:01Z Indexed on 2010/04/15 20:23 UTC
Read the original article Hit count: 446

Filed under:
|

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).

In c#, you can do this:

public void DoSomething() {
    new MyHelper().DoIt(); // works just fine
}

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

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?

© Stack Overflow or respective owner

Related posts about c#-to-vb.net

Related posts about vb.net