Is there a way for me to make a function call on a new instance of a variable without first declaring it?
So for example in Java you could do:
new foo().bar(parameters);
I’ve tried something similar in Visual Basic, but it’s a syntax error. For the moment I’m creating a variable and then running the function.
dim instance as new foo()
instance.bar(parameters)
Is there something I can do similarly to the Java code above?
Not exactly. You can do so in a larger expression by surrounding the instantiation in parenthesis, for instance:
Or, in fact, while I find it more confusing, you don’t actually even need the parenthesis around the instantiation:
However, if you want to just call a method like that, the only way I know of is to wrap in in a
CTypeoperator. For instance, if you had a class like this:You could call the
Showmethod like this:But, it is a bit clumsy.
Actually, SSS provided an even better answer since I posted this yesterday. Instead of wrapping it in a
CTypeoperator, you can use theCallkeyword. For instance: