3

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.

4 Answers 4

5

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Nice and simple explanation! There is really no way to make a function return multiple types in VB? :)
@gubbfett - There is no way to do it in most languages.
You can actually though I don't recommend it.
@Ciarán - The return type from your function is object. That's not two different return types.
@Ciarán - Object can be a String or a Boolean or anything. The return type is still Object - not Object or something else. As for returning a custom type - yes, I did mention that in my answer.
|
2

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

Comments

1

You can return Object, but it is considered very bad form for a function to return 2 data types.

2 Comments

@gubbfett - Because then you need to check what type was returned and convert to it. It is both error prone and adds complexity.
It is difficult to debug for a start E.g. If GetString("A") Then will fail. But more importantly it implies that the function is performing 2 tasks which should be in 2 separate functions. A function should only return one piece of information or if you want to return 2 then consider using a Structure.
0

As Oded said, you can't return more than one parameter from a function.

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.TryParse and similar methods work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.