vb.net 具有多个输出的 VB 函数 - 结果分配
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16436925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB function with multiple output - assignment of results
提问by Intelligent-Infrastructure
I know there's no straightforward way for multiple assignment of function in VB
, but there's my solution - is it good, how would you do it better?
我知道在VB
.
What I need (how would I do it in python, just an example)
我需要什么(我将如何在 python 中做到这一点,只是一个例子)
def foo(a) ' function with multiple output
return int(a), int(a)+1
FloorOfA, CeilOfA = foo(a) 'now the assignment of results
How I do it in VB:
我如何在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)
采纳答案by dotNET
For future readers, VB.NET 2017 and above now supports value tuples as a language feature. You declare your function as follows:
对于未来的读者,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
And you call it like this:
你这样称呼它:
Dim MyHSVColor = ColorToHSV(clr)
MsgBox(MyHSVColor.hue)
Note how VB.NET creates public property named hue
inferred from the return type of the called function. Intellisense too works properly for these members.
请注意 VB.NET 如何创建名为hue
从被调用函数的返回类型推断的公共属性。Intellisense 也适用于这些成员。
Note however that you need to target .NET Framework 4.7 for this to work. Alternately you can install System.ValueTuple
(available as NuGet package) in your project to take advantage of this feature.
但是请注意,您需要以 .NET Framework 4.7 为目标才能使其正常工作。或者,您可以System.ValueTuple
在项目中安装(作为 NuGet 包提供)以利用此功能。
回答by Steve
Your solution works and it is an elegant way to return multiple results, however you could 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 is logical to think to a class that could return all the values required (Expecially if these values are of different dataTypes)
如果你有很多结果要返回,那么考虑一个可以返回所有所需值的类是合乎逻辑的(特别是如果这些值是不同的数据类型)
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
回答by Rajaprabhu Aravindasamy
The methodology you are using is a good one, by the way, you can pass the required variable as a reference
to your subroutine
inorder to make your code
more cleaner.
您使用的方法是一个很好的方法,顺便说一下,您可以将所需的变量作为 a 传递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
回答by IvanH
Maybe 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 by Tuple.Create (thanks to @mbomb007)
编辑:用 Tuple.Create 替换新元组(感谢@mbomb007)