Why can’t I start a line using a parenthesis followed by the keyword new?? For example:
(New <custom_obj>).foo(var)
In that case is obvious that I’m trying to avoid creating a named instance of the the <custom_obj> because I know that I’ll only be using it at that sentence.
Note that actually creating a named instance is not a problem for me… I just wanna know the reason why this is not possible.
Do you want a language lawyer answer? Look in the VB.NET Language Specification, beginning at 10. Statements. The simplest answer is that the word “Expression” does not appear on that page. NewExpression is explicitly distinct from InvocationExpression, so you can use an InvocationExpression in an InvocationStatement, but you can only use a NewExpression in a LocalDeclarationStatement or in the context of some greater expression.
More handwavingly, the VB.NET syntax is designed to be easy to read, write and parse, and one way it accomplishes this is by being careful about what’s a Statement and what’s an Expression. Python is similar — an assignment is a statement and not an expression. There’s no reason for every language to be exactly like C.
But, as chibacity points out, you can work around this with the
Callstatement:will compile successfully. Try to make your classes meaningful, instead.