In C# we have:
if (int.TryParse(someString, out var i)) {
// do something with i
}
Can I do the same in Visual Basic? if not what's the most succinct we can get?
In C# we have:
if (int.TryParse(someString, out var i)) {
// do something with i
}
Can I do the same in Visual Basic? if not what's the most succinct we can get?
If I understand the intent of this question correctly, it is not asking about the exact syntax for passing by-reference parameters (a feature that VB.NET has always supported), but rather whether it is possible to declare a variable inline with the parameter list for a method call, as in the C# feature provided from C# 7 and later.
As such, the previously-proposed duplicate does not seem to address this question. That question was asking about a VB.NET-supported distinction between by-reference parameters that don't need to be initialized before passing and by-reference parameters that do, i.e. out
vs. ref
.
This question is asking about something else entirely.
Unfortunately, VB.NET does not provide a syntax that would allow local variables to be declared inline with a method call. Further, while there are several open Github issues that ask for this feature (see e.g. #60, #159, and #331), it appears unlikely that it would ever be added. Per a blog post from March 11, 2020, Microsoft has stated that "Going forward, we do not plan to evolve Visual Basic as a language". Presumably, new additions to the language syntax would fall under the category of "evolving the language", so method-call-inlined variable declarations would be ruled out.
See also Microsoft Plots the End of Visual Basic for additional context.
Sorry about that. :(