5

In C# I can do this:

new SomeObjectType("abc", 10);

In other words, I can call new without assigning the created instance to any variable. However, in VB.Net it seems I cannot do the same thing.

New SomeObjectType("abc", 10) ' syntax error

Is there a way to do this in VB.Net?

dcp
  • 54,410
  • 22
  • 144
  • 164
  • Does this answer your question? [Constructing an object and calling a method without assignment in VB.Net](https://stackoverflow.com/questions/2648700/constructing-an-object-and-calling-a-method-without-assignment-in-vb-net) – Notts90 Dec 18 '19 at 11:15

4 Answers4

3

The following works on the Mono VB compiler (vbnc, version 0.0.0.5914, Mono 2.4.2 - r):

Call New SomeObjectType("abc", 10)

Notice the required Call.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • @dcp: I compiled it before posting. (Though to be honest I’m on OS X here so I used the Mono compiler.) – Konrad Rudolph Jun 10 '10 at 15:32
  • Hmmm, interesting. Can you try to compile this (outer quotes added for clarity): "Call New Exception("abc")" I'm on XP with VS 2008, and that won't compile. Maybe it's a "feature" of Mono :). – dcp Jun 10 '10 at 15:41
  • @dcp: That works, too. Weird. Maybe you should report a bug/feature request on Microsoft Connect. – Konrad Rudolph Jun 10 '10 at 15:50
  • @dcp `Call New Exception("abc")` doesn't compile for me either. The editor autocompletes to `Call New Exception("abc")()` and then the compiler says `Error 1 '{' expected.` I also have VS2008 on XP – MarkJ Jun 10 '10 at 17:00
  • 1
    Hmm, works fine for me, VS 2013, and [here](http://stackoverflow.com/questions/2648700/constructing-an-object-and-calling-a-method-without-assignment-in-vb-net) it is an accepted answer – Pavel K Mar 12 '15 at 10:53
  • 1
    @PavelK: That other question is about **calling a method** on the newly constructed object. `Call (New SomeObjectType("abc", 10)).ToString()` compiles for me, just `Call New SomeObjectType("abc", 10)` doesn't (VS 2019). – Heinzi Jan 20 '20 at 13:23
2

See the answers to this other SO Answer

So this should work:

With New SomeObjectType("abc", 10)
End With
Community
  • 1
  • 1
Dog Ears
  • 9,637
  • 5
  • 37
  • 54
  • I don't think call won't work since I'm not calling any method, only the constructor. With doesn't really save me much, I may as well just assign it to a variable. Anyway, I guess it's not possible with VB.Net, thanks for the link though. – dcp Jun 10 '10 at 15:16
  • Sorry it was one of the other answers I was looking at particularly, I'll update my link, the answer has already been updated to reflect the use of With .. End With – Dog Ears Jun 10 '10 at 15:19
1

One can define a Sub to discard the constructed object:

Sub gobble(dummy As Object)
End Sub

Then call the constructor as follows:

gobble(New SomeClass())

I'm using this approach in tests when I test exceptions in constructors. I construct the object in a lambda and pass that lambda to a function that checks for the Exception. Fits nicely on a line.

assertThrows(Of ArgumentOutOfRangeException)(Sub() gobble(New ClassUnderTest("stuff")))
Patrick Böker
  • 3,173
  • 1
  • 18
  • 24
0

That should be the correct syntax, e.g.

Dim name As New String
Dim url As New Uri("http://www.somedomain.com")

Have you got some more code where this is happening?

Matthew Abbott
  • 60,571
  • 9
  • 104
  • 129
  • I think you misunderstood my question. I want to be able to call New without assigning its value to a variable (e.g. in your code above, I want to be able to just do New Uri("http://www.somedomain.com") without the "Dim url As" part preceeding it. – dcp Jun 10 '10 at 14:50
  • Yeah, but to do what with it? Doing New Uri("http://www.somedomain.com") without assignment or member access is pretty useless... – Matthew Abbott Jun 10 '10 at 14:56
  • I disagree. It's useful when we need to do failure testing for a constructor. For example, if the constructor cannot take a null argument for its first argument, then we can write a failure test by doing "new SomeObject(null)". There's no need to create a variable here because we are just testing that the expected exception is thrown. Have you never used NUnit or other testing frameworks before? – dcp Jun 10 '10 at 14:59
  • I see now, I've never done failure tests like that, always through assignment. And yes, I use NUnit for all my project work. – Matthew Abbott Jun 10 '10 at 15:16
  • Abbot - It's not a big deal, it's certainly not going to kill me to declare the variable, it's just convenience thing :). And besides, I try to learn something new every day ;). – dcp Jun 10 '10 at 15:18