vb.net 函数返回字符串或布尔值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13878367/
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
Function return string or boolean
提问by gubbfett
I want a function to return a String or Boolean. Something like this:
我想要一个返回字符串或布尔值的函数。像这样的东西:
Public Function GetString(Byval What As String) 'As... someting?
If (What = "A") Then
Return "String to return"
Else if (What = "B") Then
Return True
End If
Return False 'Nothing to return
End Function
How can i now do this? If i ask like
我现在该怎么做?如果我问
If GetString("A") Then
MsgBox(GetString())
End IF
...it returns a string and of course it gives an error on converting string to bool. I could always return strings and checks it lengths, but it feels bad. Or maybe I'm just into PHP too much?
...它返回一个字符串,当然它会在将字符串转换为 bool 时出错。我总是可以返回字符串并检查它的长度,但感觉很糟糕。或者也许我只是太喜欢 PHP 了?
But is there a way to do it more like this? If i ask for "B" i know it would return a bool, if i ask for "A" i want to alert the string if there was any and so on.
但是有没有办法做到更像这样?如果我要求“B”,我知道它会返回一个布尔值,如果我要求“A”,我想提醒字符串是否有任何等等。
回答by Oded
How can i now do this?
我现在该怎么做?
You can't.
你不能。
A function can only return one type, not multiple.
一个函数只能返回一种类型,不能返回多种类型。
You can return a custom type that contains a string and a boolean.
您可以返回包含字符串和布尔值的自定义类型。
回答by Ccorock
I would use an Array list. You can store whatever type you need in the list and then parse it on the return. This is really not best practice as explained above, but when you gotta get things done... The end justifies the means. Not recommended.
我会使用一个数组列表。您可以在列表中存储您需要的任何类型,然后在返回时解析它。如上所述,这确实不是最佳实践,但是当您必须完成任务时......最终证明手段是合理的。不建议。
Public Function GetString(Byval What As String) As ArrayList
Dim b as boolean = True
dim myArrayList as Arraylist = New ArrayList
If (What = "A") Then
ArrayList.Add("String to return")
Else if (What = "B") Then
ArrayList.Add(b)
End If
Return False 'Nothing to return
End Function
Proof of concept below:
概念证明如下:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim a As Boolean = True
Dim myarraylist As ArrayList = New ArrayList
myarraylist.Add(a)
myarraylist.Add("g")
Debug.WriteLine(myarraylist.GetType.ToString)
Debug.WriteLine(myarraylist(0).GetType.ToString)
Debug.WriteLine(myarraylist(1).GetType.ToString)
If myarraylist(0).GetType.ToString = "System.string" Then
Debug.WriteLine("Function returned a String")
ElseIf myarraylist(0).GetType.ToString = "System.boolean" Then
Debug.WriteLine("Function returned a Boolean")
End If
End Sub
回答by Ciarán
You can return Object, but it is considered very bad form for a function to return 2 data types.
您可以 return Object,但对于返回 2 种数据类型的函数来说,它被认为是非常糟糕的形式。
回答by Anthony
As Odedsaid, you can't return more than one parameter from a function.
正如Oded所说,一个函数不能返回多个参数。
It is not too clear what you are doing from your code example, but you may look into passing parameters by reference. As pointed out in the answer there, passing a parameter by reference is useful for:
从您的代码示例中不太清楚您在做什么,但您可以研究通过引用传递参数。正如那里的答案所指出的,通过引用传递参数对于以下情况很有用:
when you want to return a state or status of an operation plus the result from the operation.
当您想要返回操作的状态或状态以及操作的结果时。
This is how int.TryParseand similar methods work.
这就是int.TryParse类似方法的工作原理。

