Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有没有办法在VB.Net函数中返回多个类型?(与PHP内部函数不同,不是元组)_.net_Vb.net - Fatal编程技术网

有没有办法在VB.Net函数中返回多个类型?(与PHP内部函数不同,不是元组)

有没有办法在VB.Net函数中返回多个类型?(与PHP内部函数不同,不是元组),.net,vb.net,.net,Vb.net,那么您知道PHP内部函数在函数失败时通常返回布尔值FALSE,或者在函数成功时返回其他数据类型吗?这在VB.Net中是可能的吗 例如,让我们在这里说这个简单的代码 Public Function TrySomething(ByVal Param As String) \\what would go here?? If Param <> "MAGICWORD" Then Return False Else Return "You Win!

那么您知道PHP内部函数在函数失败时通常返回布尔值
FALSE
,或者在函数成功时返回其他数据类型吗?这在VB.Net中是可能的吗

例如,让我们在这里说这个简单的代码

Public Function TrySomething(ByVal Param As String) \\what would go here??
    If Param <> "MAGICWORD" Then
        Return False
    Else
        Return "You Win!"
    End If
End Function
Public Function TrySomething(ByVal参数作为字符串)\\此处将显示什么??
如果参数为“MAGICWORD”,则
返回错误
其他的
返回“你赢了!”
如果结束
端函数

你看,我想在函数失败时返回一个布尔值false,在函数工作时返回一个字符串。有什么想法吗?我到处都找过了,当我搜索“返回多个类型”时,我只找到了Tuple。

这对您很有用:

Public Function TrySomething(ByVal Param As String) As Object
If Param <> "MAGICWORD" Then
    Return False
Else
    Return "You Win!"
End If
End Function

您可以返回一个对象。在VBA中,您将返回一个变量来处理这种情况。

如果确实需要返回多个值,请为结果创建一个类。 (我是C#guy,所以没有VB,抱歉)


你可以用丹说的东西。但是,您必须在调用例程中检查返回的类型

Sub Checkit()
    Dim ret As Object = TrySomething("MAGICWORD")
    If TypeOf ret Is Boolean Then
        'dosomething
        MsgBox("Nope!")
    End If
    If TypeOf ret Is String Then
        'dosomethingelse
        MsgBox("Yes, you won!")
    End If

End Sub

Public Function TrySomething(ByVal Param As String) As Object
    If Param <> "MAGICWORD" Then
        Return False
    Else
        Return "You Win!"
    End If
End Function
Sub-Checkit()
Dim ret As Object=TrySomething(“MAGICWORD”)
如果ret的TypeOf是布尔值,那么
”“什么事
MsgBox(“不!”)
如果结束
如果ret的类型为String,则
“多索美辛格勒斯
MsgBox(“是的,你赢了!”)
如果结束
端接头
作为对象的公共函数TrySomething(ByVal参数作为字符串)
如果参数为“MAGICWORD”,则
返回错误
其他的
返回“你赢了!”
如果结束
端函数
我不会那样做,我会创建一个结构或类来处理我想要的东西。像这样:

Sub Checkit()
    Dim ret As TryReturnStruct = TrySomething("MAGICWORD")
    MsgBox(ret.Message)
    If ret.Success Then
        MsgBox("Do you want to play again?")
    End If
End Sub

Structure TryReturnStruct
    Public Success As Boolean
    Public Message As String
End Structure

Public Function TrySomething(ByVal Param As String) As TryReturnStruct
    Dim ret As New TryReturnStruct

    If Param <> "MAGICWORD" Then
        ret.Success = False
        ret.Message = "Nope, not yet!"
    Else
        ret.Success = True
        ret.Message = "You Win!"
    End If
    Return ret
End Function
Sub-Checkit()
Dim ret As TryReturnStruct=TrySomething(“MAGICWORD”)
MsgBox(ret.Message)
如果成功的话
MsgBox(“你想再玩一次吗?”)
如果结束
端接头
结构TryReturnStruct
作为布尔函数的公共成功
作为字符串的公共消息
端部结构
作为TryReturnStruct的公共函数TrySomething(ByVal参数作为字符串)
Dim ret作为新的TryReturnStruct
如果参数为“MAGICWORD”,则
ret.Success=False
ret.Message=“不,还没有!”
其他的
ret.Success=True
ret.Message=“你赢了!”
如果结束
回程网
端函数

如果我要使用一个类,我会让它像这样:

 Dim a As Boolean = TrySomething("")
 Dim b As String = TrySomething("MAGICWORD")
Public Class MyReturnType
    Private _value As Object
    Private _valueType As Type

    Public Sub New()
        _value = New Object
        _valueType = GetType(Object)
    End Sub

    Public Sub New(value As Object, valueType As Type)
        _value = value
        _valueType = valueType
    End Sub

    Public ReadOnly Property Value As Object
        Get
            Return _value
        End Get
    End Property

    Public ReadOnly Property ValueType As Type
        Get
            Return _valueType
        End Get
    End Property
End Class
然后你可以:

Public Function TrySomething(ByVal Param As String) As MyReturnType
    If Param <> "MAGICWORD" Then
        Return New MyReturnType(False, GetType(Boolean))
    Else
        Return New MyReturnType("You Win!", GetType(String))
    End If
End Function

然而,我个人认为这不是一个好的设计。如果需要处理多个类型,请使用重载或多个函数。在您的例子中,这是一个简单的“值或半身像”情况,因此我只会抛出一个异常并处理它,这是您在.NET中应该做的。

代码有一个缺陷:当执行dim a2作为boolean=TrySomething(“我想引发异常”)时会发生什么?是否需要字符串将“False”结果作为bool。正如Stefan在下面提到的,您需要在调用例程中处理返回类型。对于上述情况,最简单的可能是在“False”上返回null。返回False而不是实值实际上是一种糟糕的做法,这也是我们很多人不太喜欢php的原因之一。一个更好的解决方案是抛出一个异常。如果可行,我会说这就是答案。我已经厌倦了检查对象类型。看起来两种方法都差不多:除非使用对象类,否则不必声明结构。嗯…全方位的投票。
Public Class MyReturnType
    Private _value As Object
    Private _valueType As Type

    Public Sub New()
        _value = New Object
        _valueType = GetType(Object)
    End Sub

    Public Sub New(value As Object, valueType As Type)
        _value = value
        _valueType = valueType
    End Sub

    Public ReadOnly Property Value As Object
        Get
            Return _value
        End Get
    End Property

    Public ReadOnly Property ValueType As Type
        Get
            Return _valueType
        End Get
    End Property
End Class
Public Function TrySomething(ByVal Param As String) As MyReturnType
    If Param <> "MAGICWORD" Then
        Return New MyReturnType(False, GetType(Boolean))
    Else
        Return New MyReturnType("You Win!", GetType(String))
    End If
End Function
Sub DoSomethingWithReturnValue()
    Dim returnValue As MyReturnType = TrySomething("MAGICWORD")
    If returnValue.ValueType = GetType(Boolean) Then
        DoSomethingWithABoolean(CType(returnValue.Value, Boolean))
    Else
        DoSomethingWithAString(CType(returnValue.Value, String))
    End If
End Sub