Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function return string or boolean

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?

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.

like image 304
gubbfett Avatar asked Dec 04 '25 17:12

gubbfett


1 Answers

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.

like image 88
Oded Avatar answered Dec 06 '25 07:12

Oded



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!