Execute a function in VB.NET without first declaring an instance of the class
vb.net
Solution
Not exactly. You can do so in a larger expression by surrounding the instantiation in parenthesis, for instance:
MessageBox.Show((New String("y"c, 1)).ToUpper())
Or, in fact, while I find it more confusing, you don't actually even need the parenthesis around the instantiation:
MessageBox.Show(New String("y"c, 1).ToUpper())
However, if you want to just call a method like that, the only way I know of is to wrap in in a `CType` operator. For instance, if you had a class like this:
Private Class Test
Public Sub Show()
MessageBox.Show("Hello")
End Sub
End Class
You could call the `Show` method like this:
CType(New Test(), Test).Show()
But, it is a bit clumsy.
Actually, SSS provided an even better answer since I posted this yesterday. Instead of wrapping it in a `CType` operator, you can use the `Call` keyword. For instance:
Call New Test().Show()
Problem
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?