2013-05-08 32 views
4

我知道VB中沒有直接的函數多任務分配方式,但是我的解決方案是 - 它是否好,你會如何做得更好?具有多輸出功能的VB函數 - 賦值結果

我需要(我會怎麼做它在Python,只是一個例子)

def foo(a) ' function with multiple output 
    return int(a), int(a)+1 

FloorOfA, CeilOfA = foo(a) 'now the assignment of results 

我怎麼做,在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) 
+0

當'nA'已經是'Integer'時,沒有理由使用'CInt(nA)'。 – mbomb007 2017-09-06 18:35:13

回答

1

對於未來的讀者,VB.NET 2017年及以上現支持值元組作爲語言功能。你聲明你的功能如下:

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 

你這樣稱呼它:

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

注VB.NET如何創造公共財產名爲hue從被調用函數的返回類型推斷。智能感知也適用於這些成員。

但是請注意,您需要將.NET Framework 4.7作爲目標才能正常工作。或者,您可以在項目中安裝System.ValueTuple(作爲NuGet軟件包提供)以利用此功能。

5

您的解決方案的作品,它是一種優雅的方式以返回多個結果,但您也可以嘗試使用此功能

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 

並呼叫

foo2(nA, CeilOfA, FloorOfA)  

如果你有很多的成績來回報這是合乎邏輯去思考的一類,可以返回所需的全部值(Expecially如果這些值是不同的數據類型)

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 
+0

+1相配的想法。 – 2013-05-08 09:37:11

+0

和這樣一個基本的用途是真的需要實例化一個類?也許'結構'就夠了? – 2013-05-08 10:16:47

+0

與返回數組沒有多大區別。但你是對的,只有兩個參數和整數類型,你可以用你的解決方案或通過預期的結果通過參考。當你需要處理複雜的數據時,事情會變得更加複雜。這僅僅是一個例子(我承認,有點不太可能) – Steve 2013-05-08 10:21:41

1

你的方法使用是一個很好的方式,順便說一下,您可以將所需的變量作爲reference傳遞給您的subroutine,以使您的code更清潔。

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 
3

也許你可以使用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 

編輯:新的元組由Tuple.Create更換(感謝@ mbomb007)

+0

我不喜歡它......元組似乎很重,我只需要兩個整數。 +在'with'中調用子程序foo多少次,一次或兩次? – 2013-05-08 10:14:49

+0

@智能基礎設施:子程序只被調用一次。'用'代替'輸出'聲明。元組允許避免被認爲不是很好的實踐的byref參數。您可以在您的算法中使用.item1,.item2,從而避免FloorOfA,CeilOfA聲明。 – IvanH 2013-05-08 11:07:10

+0

我喜歡使用Tuple,但我不會在這裏使用「With」。添加一層嵌套,僅用於訪問多個返回結果。我會將結果分配給本地變量。 Dim輸出= foo(nA)/ Dim FloorOfA = Output.Item1'。像有問題的代碼,但使用Tuple而不是數組。 – ToolmakerSteve 2013-12-09 22:02:53

相關問題