Multiple Output VB Function - Assign Results

I know that there is no easy way for multiple function assignment in VB , but my solution there is good, how would you do it better?

What I need (how to do it in python, just an example)

 def foo(a) ' function with multiple output return int(a), int(a)+1 FloorOfA, CeilOfA = foo(a) 'now the assignment of results 

How do I do this in VB:

 Public Function foo(ByVal nA As Integer) As Integer() ' function with multiple output Return {CInt(nA),CInt(nA)+1} End Function Dim Output As Integer() = foo(nA) 'now the assignment of results Dim FloorOfA As Integer = Output(0) Dim CeilOfA As Integer = Output(1) 
+9
source share
4 answers

For future readers, VB.NET 2017 and above now supports tuples of values ​​as a language function. You declare your function as follows:

 Function ColorToHSV(clr As System.Drawing.Color) As (hue As Double, saturation As Double, value As Double) Dim max As Integer = Math.Max(clr.R, Math.Max(clr.G, clr.B)) Dim min As Integer = Math.Min(clr.R, Math.Min(clr.G, clr.B)) Dim h = clr.GetHue() Dim s = If((max = 0), 0, 1.0 - (1.0 * min / max)) Dim v = max / 255.0 Return (h, s, v) End Function 

And you call it that:

 Dim MyHSVColor = ColorToHSV(clr) MsgBox(MyHSVColor.hue) 

Notice how VB.NET creates an open property with the name hue inferred from the return type of the called function. Intellisense also works correctly for these contributors.

However, note that you need to target the .NET Framework 4.7 for this to work. Alternatively, you can install System.ValueTuple (available as a NuGet package) in your project to take advantage of this feature.

+6
source

Your solution works and this is an elegant way to return multiple results, however you can also try with this

 Public Sub foo2(ByVal nA As Integer, ByRef a1 As Integer, ByRef a2 As Integer) a1 = Convert.ToInt32(nA) a2 = Convert.ToInt32(nA) +1 End Sub 

and call with

 foo2(nA, CeilOfA, FloorOfA) 

If you have many results to return, it makes sense to think of a class that could return all the necessary values ​​(especially if these values ​​have different data types)

 Public Class CalcParams Public p1 As Integer Public p2 As String Public p3 As DateTime Public p4 As List(Of String) End Class Public Function foo2(ByVal nA As Integer) As CalcParams Dim cp = new CalcParams() cp.p1 = Convert.ToInt32(nA) ....... Return cp End Function 
+8
source

Perhaps you can use Tuple :

 Public Function foo(ByVal nA As Integer) As Tuple(Of Integer,Integer) ' function with multiple output Return Tuple.Create(CInt(nA),CInt(nA)+1) End Function Dim FloorOfA, CeilOfA As Integer With foo(nA) 'now the assignment of results FloorOfA =.item1 CeilOfA = .item2 End With 

Edit: new tuple replace Tuple.Create (thanks @ mbomb007)

+3
source

The methodology you use is good, by the way, you can pass the required variable as reference to your subroutine to make your code cleaner.

 Dim FloorOfA As Integer Dim CeilOfA As Integer Call foo(10.5, FloorOfA, CeilOfA) Public Sub foo(ByVal xVal As Integer, ByRef x As Integer, ByRef y As Integer) x = CInt(xVal) y = CInt(xVal) + 1 End Sub 
+2
source

Source: https://habr.com/ru/post/1479698/


All Articles