Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Why isn't this allowed in VB.NET:

New SqlCommand("some string", someSqlConnection).ExecuteNonQuery()

Why would the language have been designed to require that this be used instead:

Dim com As New SqlCommand("some string", someSqlConnection)
com.ExecuteNonQuery()

It doesn't seem to me like it would have taken much time at all to implement. Am I wrong? Is there some theoretical advantage this would give a language?

EDIT

Please read: I thought this language feature was not supported in VB.NET. I had forgotten that I had used it in the past, and so when lines like the first one above would break, I did not expect there would be a work-around like in the accepted answer.

...Although, looking back at it again, there seems to be trouble doing this when it's off by itself on a line of code, and not used as a function argument or something. I bet that's probably part of why I originally posted this. I wonder why this in particular wouldn't be allowed, but I'm asking a question on SO to see if there's a similar work-around for that...

This is not a question about how to use unnamed objects on a single line; this was a question about why a language designer would intentionally choose to forgo such a feature in a language like this. Was there a lingual advantage to not allowing this feature, or would it just take unexpectedly long to implement?

There was a misunderstanding, but that does not change the conceptual nature of the original question, and it does not change the fact that it was searching for a logical reason to omit such a feature.

The bottom line is this: This is a site about "conceptual questions about software development". This almost immediately implies trace amounts of opinion, subjectiveness, etc - as specifically noted in the very same flag this question was put under. The rule is to not push it too far, and my question did not. Please remove the flag. Thank you.

share|improve this question
1  
I suspect the close votes are because you asked why a language which allows unnamed, one-line objects to be used doesn't allow unnamed, one-line objects to be used. –  Carson63000 Feb 18 at 2:08
1  
Perhaps the question should be edited to ask specifically how to write the functionality in question and migrated to Stackoverflow? –  Locke Feb 18 at 15:07
    
@Panzercrisis I understand. My suggestion was aimed more at helping others who might not know how to use unnamed objects in VB. The syntax - if you aren't from a VB background - can be somewhat counter-intuitive. –  Locke Feb 18 at 20:56
1  
it would have taken much time at all to implement little things like this are actually very hard to implement. Language grammar has a ripple effect. If you change it to make one thing work you break dozens of other things. I fear this is such a case. –  Mathew Foscarini Mar 10 at 13:58

1 Answer 1

up vote 14 down vote accepted

VB does have this functionality, however the syntax is a bit different (obviously) than C#. Try (New SqlCommand("some string", someSqlConnection)).ExecuteNonQuery().

Basically, adding the parenthesis around the instantiation of the object tells it to first create the object, then call the method. The code in the question, on the other hand, is trying to tell it to create an object whose type is SqlCommand("some string", someSqlConnection)).ExecuteNonQuery() (which obviously isn't an object type).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.