Home All Groups Group Topic Archive Search About

Function with Two Values

Author
4 Dec 2006 11:20 AM
William Foster
Good evening all,

I was wondering if there is a way to do a function or different sub
routine that returns two or more values.

I usually use a function to return a boolean value, however, this
sometimes requires me to undertake two functions.  I would like to do it
in one operation.

I have thought that I could get the function to reutrn a string, then
simply break-up the string into seperate chunks that mean different
things, but I thought I would ask to see if there is a neater way to do
this.

As an example I would like to return a boolean and an integer from a
function, i.e:

Private Sub Query()

     Dim Answer, Answer_Part_1, Answer_Part_2 as String
     Answer = Working_Out(Blah, Blah, Blah)
     Answer_Part_1 = Left(Answer,3)
     Answer_Part_2 = Right(Answer,3)

End Sub

Private Function Working_Out(ByVal Blah1 as String,ByVal Blah2 as
String,ByVal Blah3 as String) as String

     Dim Return_String as String
     Return_String = "Yes" & 123

     Working_Out = Return_String

End Function

Any assistance you may be able to provide would be greatly appreciated.

Yours sincerely,

William Foster

*** Sent via Developersdex http://www.developersdex.com ***

Author
4 Dec 2006 11:25 AM
Coder
You could pass them byref....


Show quoteHide quote
"William Foster" <nospam@devdex.com> wrote in message
news:%234Xh3Y5FHHA.3304@TK2MSFTNGP05.phx.gbl...
> Good evening all,
>
> I was wondering if there is a way to do a function or different sub
> routine that returns two or more values.
>
> I usually use a function to return a boolean value, however, this
> sometimes requires me to undertake two functions.  I would like to do it
> in one operation.
>
> I have thought that I could get the function to reutrn a string, then
> simply break-up the string into seperate chunks that mean different
> things, but I thought I would ask to see if there is a neater way to do
> this.
>
> As an example I would like to return a boolean and an integer from a
> function, i.e:
>
> Private Sub Query()
>
>     Dim Answer, Answer_Part_1, Answer_Part_2 as String
>     Answer = Working_Out(Blah, Blah, Blah)
>     Answer_Part_1 = Left(Answer,3)
>     Answer_Part_2 = Right(Answer,3)
>
> End Sub
>
> Private Function Working_Out(ByVal Blah1 as String,ByVal Blah2 as
> String,ByVal Blah3 as String) as String
>
>     Dim Return_String as String
>     Return_String = "Yes" & 123
>
>     Working_Out = Return_String
>
> End Function
>
> Any assistance you may be able to provide would be greatly appreciated.
>
> Yours sincerely,
>
> William Foster
>
> *** Sent via Developersdex http://www.developersdex.com ***
Author
4 Dec 2006 11:39 AM
Stephany Young
There are a number of ways to achieve this.

One way is to use an array to as the return value but this would require
that the type of each element be the same, so as a variation you could use
an Arraylist:

  Private Sub Query()

    Dim Answer As Arraylist = Working_Out(Blah, Blah, Blah)

    Dim Answer_Part_1 As Boolean = CType(Answer(0), Boolean)

    Dim Answer_Part_2 As String = CType(Answer(1), String)

  End Sub

  Private Function Working_Out(ByVal Blah1 as String,ByVal Blah2 as
String,ByVal Blah3 as String) as Arraylist

    Dim Answer As New Arraylist

    Answer.Add(True)

    Answer.Add("123")

    Return Answer

  End Function


Show quoteHide quote
"William Foster" <nospam@devdex.com> wrote in message
news:%234Xh3Y5FHHA.3304@TK2MSFTNGP05.phx.gbl...
> Good evening all,
>
> I was wondering if there is a way to do a function or different sub
> routine that returns two or more values.
>
> I usually use a function to return a boolean value, however, this
> sometimes requires me to undertake two functions.  I would like to do it
> in one operation.
>
> I have thought that I could get the function to reutrn a string, then
> simply break-up the string into seperate chunks that mean different
> things, but I thought I would ask to see if there is a neater way to do
> this.
>
> As an example I would like to return a boolean and an integer from a
> function, i.e:
>
> Private Sub Query()
>
>     Dim Answer, Answer_Part_1, Answer_Part_2 as String
>     Answer = Working_Out(Blah, Blah, Blah)
>     Answer_Part_1 = Left(Answer,3)
>     Answer_Part_2 = Right(Answer,3)
>
> End Sub
>
> Private Function Working_Out(ByVal Blah1 as String,ByVal Blah2 as
> String,ByVal Blah3 as String) as String
>
>     Dim Return_String as String
>     Return_String = "Yes" & 123
>
>     Working_Out = Return_String
>
> End Function
>
> Any assistance you may be able to provide would be greatly appreciated.
>
> Yours sincerely,
>
> William Foster
>
> *** Sent via Developersdex http://www.developersdex.com ***
Author
4 Dec 2006 12:37 PM
guy
return a structure containing a boolean and the other value that you need to
return

hth

guy

Show quoteHide quote
"William Foster" wrote:

> Good evening all,
>
> I was wondering if there is a way to do a function or different sub
> routine that returns two or more values.
>
> I usually use a function to return a boolean value, however, this
> sometimes requires me to undertake two functions.  I would like to do it
> in one operation.
>
> I have thought that I could get the function to reutrn a string, then
> simply break-up the string into seperate chunks that mean different
> things, but I thought I would ask to see if there is a neater way to do
> this.
>
> As an example I would like to return a boolean and an integer from a
> function, i.e:
>
> Private Sub Query()
>
>      Dim Answer, Answer_Part_1, Answer_Part_2 as String
>      Answer = Working_Out(Blah, Blah, Blah)
>      Answer_Part_1 = Left(Answer,3)
>      Answer_Part_2 = Right(Answer,3)
>
> End Sub
>
> Private Function Working_Out(ByVal Blah1 as String,ByVal Blah2 as
> String,ByVal Blah3 as String) as String
>
>      Dim Return_String as String
>      Return_String = "Yes" & 123
>
>      Working_Out = Return_String
>
> End Function
>
> Any assistance you may be able to provide would be greatly appreciated.
>
> Yours sincerely,
>
> William Foster
>
> *** Sent via Developersdex http://www.developersdex.com ***
>
Author
4 Dec 2006 2:13 PM
Tom Leylan
William:  Think "object oriented".  Never set yourself up with additional
work by (among other things) concatenating return values requiring you to
immediately break them apart again.

As was pointed out (and is often done) you can pass the variables Answer_1
and Answer_2 by reference to the function which will assign them (leaving
the return value available for some other use.)  Better yet though is Guy's
reply.  If you "think OOP" (and look at the .Net framework (and other
frameworks for that matter)) you will see they often return "objects" or as
Guy is pointing out a structure (sort of a lightweight object.)  They can be
expanded (if you need a 3rd or 4th value) and they are self-documenting as
the "parts" you refer to would be named something like Answer.Text and
Answer.Number.

Tom


Show quoteHide quote
"William Foster" <nospam@devdex.com> wrote in message
news:%234Xh3Y5FHHA.3304@TK2MSFTNGP05.phx.gbl...
> Good evening all,
>
> I was wondering if there is a way to do a function or different sub
> routine that returns two or more values.
>
> I usually use a function to return a boolean value, however, this
> sometimes requires me to undertake two functions.  I would like to do it
> in one operation.
>
> I have thought that I could get the function to reutrn a string, then
> simply break-up the string into seperate chunks that mean different
> things, but I thought I would ask to see if there is a neater way to do
> this.
>
> As an example I would like to return a boolean and an integer from a
> function, i.e:
>
> Private Sub Query()
>
>     Dim Answer, Answer_Part_1, Answer_Part_2 as String
>     Answer = Working_Out(Blah, Blah, Blah)
>     Answer_Part_1 = Left(Answer,3)
>     Answer_Part_2 = Right(Answer,3)
>
> End Sub
>
> Private Function Working_Out(ByVal Blah1 as String,ByVal Blah2 as
> String,ByVal Blah3 as String) as String
>
>     Dim Return_String as String
>     Return_String = "Yes" & 123
>
>     Working_Out = Return_String
>
> End Function
>
> Any assistance you may be able to provide would be greatly appreciated.
>
> Yours sincerely,
>
> William Foster
>
> *** Sent via Developersdex http://www.developersdex.com ***
Author
4 Dec 2006 5:06 PM
Cor Ligthert [MVP]
William,

I don't like it, but to make this thread complete

Private Sub MySub(byval mybool as boolean, byval myint as int)
    mybool = true
    myInteger  = 2
End sub

mybool as booleand = false
myInteg as int = 1
mySub(mybool,mystring)

The bool is now true and the myInt = 2,

But it is confusing to use, therefore I don't like it.

I like more the method with the objects or arrays as is told here as well.

Cor



Show quoteHide quote
"William Foster" <nospam@devdex.com> schreef in bericht
news:%234Xh3Y5FHHA.3304@TK2MSFTNGP05.phx.gbl...
> Good evening all,
>
> I was wondering if there is a way to do a function or different sub
> routine that returns two or more values.
>
> I usually use a function to return a boolean value, however, this
> sometimes requires me to undertake two functions.  I would like to do it
> in one operation.
>
> I have thought that I could get the function to reutrn a string, then
> simply break-up the string into seperate chunks that mean different
> things, but I thought I would ask to see if there is a neater way to do
> this.
>
> As an example I would like to return a boolean and an integer from a
> function, i.e:
>
> Private Sub Query()
>
>     Dim Answer, Answer_Part_1, Answer_Part_2 as String
>     Answer = Working_Out(Blah, Blah, Blah)
>     Answer_Part_1 = Left(Answer,3)
>     Answer_Part_2 = Right(Answer,3)
>
> End Sub
>
> Private Function Working_Out(ByVal Blah1 as String,ByVal Blah2 as
> String,ByVal Blah3 as String) as String
>
>     Dim Return_String as String
>     Return_String = "Yes" & 123
>
>     Working_Out = Return_String
>
> End Function
>
> Any assistance you may be able to provide would be greatly appreciated.
>
> Yours sincerely,
>
> William Foster
>
> *** Sent via Developersdex http://www.developersdex.com ***
Author
4 Dec 2006 11:16 PM
William Foster
Thank you all for your inpt on a beginner question, I will be
investigating the Structure and Answer as an Array List options when I
get home tonight.

Yours sincerely,

William Foster

*** Sent via Developersdex http://www.developersdex.com ***