syntax - Can you use unnamed, one-line objects in VB.NET without passing them as arguments or something? -


i thought remembered using unnamed, one-line objects in vb.net without passing them arguments or anything. instance:

new sqlcommand("some string", somesqlconnection) 

does not compile, , neither this:

(new sqlcommand("some string", somesqlconnection)) 

in second case, added parantheses allow call functions on object , pass return values arguments other functions, doesn't work when object or 1 of functions not being used argument function.

however compile:

somefunction(new sqlcommand("some string", somesqlconnection)) 

is there way around restriction? in other words, without wrapping function calls around them or adding more lines of code? if not, wonder why would've been left out of language?

the reason can think of why want (create instance without assinging variable) if you're interessted in side-effect caused constructor. if case, not hurt create variable dim tmp = new myobject() (what hurt constructor side-effects, that's topic).

if, however, you're going call method on object, can use call statement, like

call new sqlcommand("foo", somesqlconnection).executenonquery() 

call requiered here because expression doesn't start identifier.

using call friend here, long call method on new object. , since each object provides tostring method, use call new foo().tostring() create instance of foo in 1 line without assigning variable.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -