Is there a way to return multiple types to a VB.Net function? (not a tuple like PHP's internal functions)

So, do you know how PHP internal functions usually return boolean FALSE when a function fails, or some other data type when the function succeeds? Is this possible in VB.Net?

For example, let's say this simple code is here

 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 

You see that I want to return BOOLEAN false when the function fails and the string when the function works. Any ideas? I searched everywhere, and when I search for "returning multiple types", all I find is Tuple.

+4
source share
5 answers

This will work for you:

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

You can check the values ​​as follows:

  Dim a As Boolean = TrySomething("") Dim b As String = TrySomething("MAGICWORD") 
+6
source

You can use the object, as Dan says. But then you must check the return type in the calling procedure.

 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 

I would not go this way, I would create a structure or class to handle exactly what I wanted instead. Like this:

 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 
+6
source

You can return the object. In VBA, you will return the option to deal with this situation.

+2
source

If you really need to return multiple values, create a class for the result. (I'm a C # guy, so there is no VB, sorry)

 // A generic result type. class Result<T> { public bool Success { get; set; } public T Value { get; set; } } 

However, in most cases, when you have a “result or result failure” as the result of a call, you should probably just consider rejection as an exception.

 string MyMethod(string arg) { if (arg != "Magic") throw new ArgumentException("Argument was incorrect", "arg"); return "Hello " + arg; } 
+1
source

If I were to use a class, I would do it something like this:

 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 

Then you can:

 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 

And all is well:

 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 

Personally, however, I do not think this is a good design. If you need to handle multiple types, use overloads or multiple functions. In your case, this is a simple “value or bust” situation, so I would just throw an exception and handle it, which should do in .NET.

0
source

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


All Articles