Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function with multiple returning types at cases is it possible and how? 3

Status
Not open for further replies.

WomanPro

Programmer
Nov 1, 2012
180
GR
Hello everyone,
I have a function that returns boolean when I can it. In some cases I want to return a byte too. So I declared a structure like that
Public Structure DblOutInfos
Dim i As Byte
Dim played As Boolean
End Structure

And my function is like that
Public Function SetPcChckr2ExcludedGate(ByVal Rd As Integer, ByRef chckr As Checker) As DblOutInfos
Dim DblOutInfos As New DblOutInfos
.
.
.
return DblOutInfos

My problem is that in some cases when I call the function I want to return only the boolean type and in some others I want to return DblOutInfos. Is there a way to declare the function so that to return even the one type (boolean) even the other (DblOutInfos) similtaneously and how can I do that?
I mean something like that:
Dim DblPl, Played as boolean
if DblPl then Played = SetPcChckr2ExcludedGate(rd, chckr)
if DblPl = False then DblOutInfos = SetPcChckr2ExcludedGate(rd, chckr)
Any help will be much appreciated. Thank you so much in advanced.
 
I don't use Structures, however from what it appears that you are doing - you have a Function that you want to return different types of data depending on certain conditions determined within the Function. The problem is how do you return the different result (type).

You have defined the result types in your Structure, so as far as I can see - if you add another Field to the Structure such as [tt]Dim ResultType as String[/tt]. Once your Function has determined which value it needs to return, it sets the appropriate value in the Structure AND ResultType = "Byte" or ResultType = "Boolean" as necessary.

You can then check for this on return:

[tt]Dim x as MyStruct = MyFunc
If x.ResultType = "Byte" then
'use x.i
Else
'use x.played
End If[/tt]

I don't have VS running and so can't test this (and as I said I don't use Structures) but in principle it should do what you need.
 
Thank you softhemc. However you gave me an idea for something else I would like to ask too. It happens to have procedures that do the same thing for different data types. So is it possible to declare a procedure that gets argument of different data type each time we call it? Should I use Structures like you mentioned above for that? What is your opinion?
 
Since you need to verify the return type anyway, you could just pass it back as an Object and check its type on return. You could use the same technique going the other way when passing an unknown type into the subroutine.

Code:
Private Function MysteryReturn(Rd As Integer) As Object
    If (Rd <= 42) Then
        Dim myBool As Boolean = True
        Return myBool
    Else
        Dim myByte As Byte = 50
        Return myByte
    End If
End Function

Private Sub MysteryParameter(mystery As Object)
    If (TypeOf mystery Is Boolean) Then
        Dim myBool = DirectCast(mystery, Boolean)
        ' Work with Boolean input parameter
    ElseIf (TypeOf mystery Is Byte) Then
        Dim myByte = DirectCast(mystery, Byte)
        ' Work with Byte input parameter
    End If
End Sub

Then to use the MysteryReturn function:
Code:
Dim o As Object = MysteryReturn(2015)
If (TypeOf o Is Boolean) Then
    myBool = DirectCast(o, Boolean)
ElseIf (TypeOf o Is Byte) Then
    myByte = DirectCast(o, Byte)
End If

But ... just because you want to do something and you can do something doesn't mean you should do something.
 
I totally missed the obvious answer to your second question; the better technique would be to use method overloading to pass different parameter types into your subroutine:

Code:
Private Sub MyOverloadedSub(value As Boolean)
    ' Work with Boolean input parameter
End Sub

Private Sub MyOverloadedSub(value As Byte)
    ' Work with Byte input parameter
End Sub
 
Thank you so much DaveInIowa too!!!!
May you give me an example of overloaeding method? Could you explain me what exactly can do? Do I have to write the code N times as much as the count of the types? For example if I have 5 differente types, how many times should I write the code of the routine for each type? I haven't use it again, and I don't know. I would appreciated if you would help me.
 
The sample above is a simple example of overloading. You can have any number of the same-named subroutines as you want as long as the signature (number of parameters or type of parameters) varies. For your 5 different types, you will have to define 5 separate subroutines but you can call one of the other overloaded subroutines from the other if you can translate one parameter type to another. It's hard to give an example without knowing your particular code. Does the following example make any sense?

Code:
    Private Sub MyOverloadedSub(value As Boolean)
        ' Based on the Boolean parameter, call an overload to do the processing
        ' keeping this subroutine short
        Dim xlateToByte As Byte
        If (value) Then
            xlateToByte = 128
        Else
            xlateToByte = 0
        End If
        [red]MyOverloadedSub(xlateToByte)[/red]
    End Sub

    Private Sub MyOverloadedSub(value As Byte)
        ' This overload accepts a Byte and does the actual processing
        ' ...
    End Sub
 

Yet another way to do this, and that will allow you to "return" both the Boolean and the struct (DblOutInfos) at the same time: optional ByRef parameters. You would declare the function like so:

Public Function SetPcChckr2ExcludedGate(ByVal Rd As Integer, ByRef chckr As Checker, [red]Optional ByRef DBO As DblOutInfos = Nothing[/red]) As Boolean

Dim bRetVal as Boolean = False

If DBO IsNot Nothing Then
'Do Stuff
'Fill values of DBO. Since it is passed ByRef, the changes will be reflected in the variable passed in the function call and can be used later.
End If

Return bRetVal

End Function

Call it like so:

Dim bFuncResult As Boolean = False
Dim DBO1 As DblOutInfos = New DblOutInfos
Dim i As Integer = 0
Dim chkr As Checker = New Checker

bFuncResult = SetPcChckr2ExcludedGate(i, chkr) 'This call excludes the optional variable...will only get boolean return value in bFuncResult

bFuncResult = SetPcChckr2ExcludedGate(i, chkr, DBO1) 'This call includes the optional variable...will get boolean return value in bFuncResult *and* DBO1 will contain values assigned in the function

Hope this helps, and let me know if you need any clarification.


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Yes it does ;) I ran it step over. A question please.
I did a small change and in the msgbox I got 255 value what does that mean? What is the relation between a boolean value true and an integer of 255? Does it have to do with Ascii code or with the range of byte?

Private Sub MyOverloadedSub(ByVal value As Boolean)
' Based on the Boolean parameter, call an overload to do the processing
' keeping this subroutine short
Dim xlateToByte As Byte
If (value) Then
xlateToByte = 128
Else
xlateToByte = True
End If
MyOverloadedSub(xlateToByte)
End Sub

Private Sub MyOverloadedSub(ByVal value As Byte)
' This overload accepts a Byte and does the actual processing
' ...
MsgBox(value)
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim x = 10

'MyOverloadedSub(True)
MyOverloadedSub(False)
End Sub
 
Oh thank you Jebenson, another good solution too :). I am glad when I have alternative ways to do what I want.
 
Going back to my assembler days (so I presume iy still holds true today) - FALSE = 0 (no bits set) and TRUE = -1 (all bits set).

Depending on the data size:
[tt]Byte: -1 = 255 (all 8 bits set)[/tt]​
[tt]Word: -1 = 65535(all 16 bits set)[/tt]​
etc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top