FastAl
FastAl

Reputation: 6280

Why can't I do this - Create a .net object and call one method all on one line

(New clsViewIllus).View(MyIBaseView, enumViewSolveTypes.View, Me, , True) 

... in VB? Basically too lazy to do this:

Dim vi As New clsViewIllus
vi.View(MyIBaseView, enumViewSolveTypes.View, Me, , True)

Upvotes: 3

Views: 664

Answers (4)

Ricardo Virtudazo Jr
Ricardo Virtudazo Jr

Reputation: 351

I know the post is back in 2011, but a helpful tip for those who come across this post...

You can also use the Call statement, such as:

Call New clsViewIllus.View(MyIBaseView, enumViewSolveTypes.View, Me, , True) 

You can even set properties along the way...

Call New clsViewIllus With {.Prop1 = value}.View(MyIBaseView, enumViewSolveTypes.View, Me, , True) 

Upvotes: 3

Glennular
Glennular

Reputation: 18215

Hack Version

CType(New clsViewIllus(), clsViewIllus).View(MyIBaseView,....)

Upvotes: 0

FastAl
FastAl

Reputation: 6280

As far as I've found, with vs2008, It can't be done, unless you do something with the return value, e.g.,:

Process.Start(New ProcessStartInfo("c:\temp\out.bmp") With {.UseShellExecute = True})

but my .View above is a sub so I couldn't even assign it to a dummy object. Makes sense that you can do this in c#.

Upvotes: 0

IAbstract
IAbstract

Reputation: 19871

clsViewIllus should be a shared class. You mentioned in comments that the class has other uses and ways to use the methods. But do those other uses stay in context with the method you have defined?

If not, then you need to write another class.

Now, to your question:

Why can't I:

(New clsViewIllus).View(MyIBaseView, enumViewSolveTypes.View, Me, , True)  

Edit
Basically, it isn't allowed from what I can tell. If I can find something in the language specification, I will update. But the compiler pretty much tells you why this can't be done:

I made a mistake on my side:

I believe you can. I did it in C#. What you may be missing is the parentheses. Try this:

(New clsViewIllus()).View(MyIBaseView, enumViewSolveTypes.View, Me, , True)

Although, IMHO, it is bad form and screams for Module or Shared.

Upvotes: 1

Related Questions