4

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?

2 Answers 2

6

The magic word here is Call.

Public Sub DoSomething()
    Call (New MyHelper()).DoIt()
    Call New MyHelper().DoIt()
End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

It is magical! Nice for showing both options (with and without parentheses). +1
2

Gideon Engelberth is right about using Call. It is the best option.

Another option is to use a With statement:

With New MyHelper()
    .DoIt()
End With

1 Comment

This is actually the option you should go for it you are calling several methods on your object, for example if you use a SaveFileDialog which you are only interested in showing and collection values from, but don't need long term.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.