INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

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

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

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

(OP)
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.

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

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 Dim ResultType as String. 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:

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


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.

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

(OP)
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?

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

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.

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

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 

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

(OP)
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.

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

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
        MyOverloadedSub(xlateToByte)
    End Sub

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

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


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, Optional ByRef DBO As DblOutInfos = Nothing) 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!

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

(OP)
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

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

(OP)
Oh thank you Jebenson, another good solution too :). I am glad when I have alternative ways to do what I want.

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

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:
Byte: -1 = 255 (all 8 bits set)
Word: -1 = 65535(all 16 bits set)
etc.

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

(OP)
Thank you softhemic!!!!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close