views:

56

answers:

3

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?

A: 

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
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
Yeah, but to do what with it? Doing New Uri("http://www.somedomain.com") without assignment or member access is pretty useless...
Matthew Abbott
@Matthew Abbott - 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
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
@Matthew 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
A: 

See the answers to this other SO Answer

So this should work:

With New SomeObjectType("abc", 10)
End With
Dog Ears
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
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
A: 

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
@Konrad - That doesn't work, it won't compile.
dcp
@dcp: I compiled it before posting. (Though to be honest I’m on OS X here so I used the Mono compiler.)
Konrad Rudolph
@Konrad - 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
@dcp: That works, too. Weird. Maybe you should report a bug/feature request on Microsoft Connect.
Konrad Rudolph
@Konrad, @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