2

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?

GreyCloud
  • 3,030
  • 5
  • 32
  • 47
  • @ZoharPeled : I don't think that duplicate is quite correct. The OP is asking about the C# 7 feature of being able to declare the receiving variable directly in the parameter/method call ([see the docs](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-7#out-variables)), rather than having to declare it separately before that. The duplicate merely discusses the `out` keyword in general when used in one of your methods. – Visual Vincent Sep 03 '19 at 09:12
  • Might not be an exact duplicate, but if there's no equivalent to the `out` keyword, how can there be an equivalent to `out var`? You can always vote to reopen if you think it's not a duplicate @VisualVincent – Zohar Peled Sep 03 '19 at 09:31
  • @ZoharPeled : Well since VB.NET's "equivalent" is `ByRef` variables (`ref` in C#), they _could_ technically be made to support inline declaration. I don't think this feature exists yet, but I also haven't been very up-to-date on the new features of VB.NET. || _"You can always vote to reopen if you think it's not a duplicate"_ - I know, I just wanted to hear your reason behind flagging it first, in case it might change my mind. – Visual Vincent Sep 03 '19 at 09:39
  • only `ByRef` is an equivalent to `ref`, and not to `out` - there are similarities between the two c# keyword, but they are not the exactly the same... – Zohar Peled Sep 03 '19 at 09:44
  • @ZoharPeled : I know it's not an exact equivalent (that's why I put the word in quotation marks) but it's how VB does it and, as you say, works similarly/has some similarities to `out`. – Visual Vincent Sep 03 '19 at 09:46
  • 1
    @VisualVincent This is starting to be too long of a discussion. Please either vote to reopen or just leave it be. I don't think there's much more I can say about the (lack of) the `out` keyword (and it's latest groovy options) in VB.Net. – Zohar Peled Sep 03 '19 at 09:51
  • 3
    @GreyCloud : After updating myself on the VB.NET news I found that this has not been implemented yet, but it has been proposed a couple of times so we might see it in the future: [\[ Better support for Out Parameters by introducing Out Variables \]](https://github.com/dotnet/vblang/issues/60) and: [\[ inline variable declaration and assignment within expressions \]](https://github.com/dotnet/vblang/issues/159) and: [\[ Make VB great again! \]](https://github.com/dotnet/vblang/issues/331). – Visual Vincent Sep 03 '19 at 10:14
  • 1
    @VisualVincent Great links! Glad to know this is under consideration for VB. This is one item that is on my very short list of what I like about C#. :~) – Mary Sep 03 '19 at 19:36
  • 2
    I disagree with the closing of this question and with the logic of the premise "if you don't have out, how can you have out var?" - TryParse still works in vb even though it "doesn't have" out, so I think "does it have out var" is a perfectly reasonable question – Caius Jard Apr 17 '20 at 20:24

1 Answers1

2

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. :(

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136