|
How can I return multiple values from a custom function?
TIA | |
Share:
|
You can either create an object with a bunch of properties - and you set
those properties.
You can also have ByRef parameters in your function, and return values by
setting those parameters - so they consumer of your functin can check the
values of those parameters after the function is done.
"Nikolay Petrov" <jo******@mail.bg> wrote in message
news:e4**************@TK2MSFTNGP11.phx.gbl... How can I return multiple values from a custom function?
TIA
| | |
Return a Structure
Friend MyFunction() as MyStructure
Dim ms as MyStructure
ms.<properties>= . . . .
Return MyStructure
End Function
--
OHM ( Terry Burns )
. . . One-Handed-Man . . .
"Nikolay Petrov" <jo******@mail.bg> wrote in message
news:e4**************@TK2MSFTNGP11.phx.gbl... How can I return multiple values from a custom function?
TIA
| | |
Hi Nikolay,
In addition to the others,
Be first sure that you needed a return value.
When it is about an object you can even use a sub
Private Sub a (byval b as whateverObject)
end sub
is the same as
Private Function a (byval b as whateverObject) as whateverObject
return b
end function
I hope this helps?
Cor | | |
ByRef is only relevent for primitives in this respect. Passing a class to
function ByRef or ByVal would not affect the called functions ability to
change the state of the passed object.
--
OHM ( Terry Burns )
. . . One-Handed-Man . . .
"Marina" <so*****@nospam.com> wrote in message
news:ep**************@TK2MSFTNGP11.phx.gbl... You can either create an object with a bunch of properties - and you set those properties. You can also have ByRef parameters in your function, and return values by setting those parameters - so they consumer of your functin can check the values of those parameters after the function is done.
"Nikolay Petrov" <jo******@mail.bg> wrote in message news:e4**************@TK2MSFTNGP11.phx.gbl... How can I return multiple values from a custom function?
TIA
| | |
That is true in most cases.
However, consider this:
Dim var as Object
SomeFunction(var)
And SomeFunction sets it's parameter to a new instance of an object. In
this particular case, if the parameter is declared ByVar, 'var' is still
Nothing after the call. If it's ByRef, 'var' will be whatever SomeFunction
set it to.
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message
news:Oc**************@TK2MSFTNGP10.phx.gbl... ByRef is only relevent for primitives in this respect. Passing a class to function ByRef or ByVal would not affect the called functions ability to change the state of the passed object.
--
OHM ( Terry Burns ) . . . One-Handed-Man . . .
"Marina" <so*****@nospam.com> wrote in message news:ep**************@TK2MSFTNGP11.phx.gbl... You can either create an object with a bunch of properties - and you set those properties. You can also have ByRef parameters in your function, and return values
by setting those parameters - so they consumer of your functin can check
the values of those parameters after the function is done.
"Nikolay Petrov" <jo******@mail.bg> wrote in message news:e4**************@TK2MSFTNGP11.phx.gbl... How can I return multiple values from a custom function?
TIA
| | |
Terry,
As Marina suggests, ByRef is required for both value & reference types, as
Nikolay wants to RETURN multiple values.
Dim s As String
Dim i As Integer
Dim p As Person
CreateStuff(s, i, p)
Public Sub CreateStuff(ByRef sOut As String, _
ByRef iOut As Integer, _
ByRef pOut As Person)
sOut = "Hello World"
iOut = 5
pOut = New Person("Me")
End Sub
You are correct ByVal is only needed if you are only changing the state of a
passed object.
Hope this helps
Jay
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message
news:Oc**************@TK2MSFTNGP10.phx.gbl... ByRef is only relevent for primitives in this respect. Passing a class to function ByRef or ByVal would not affect the called functions ability to change the state of the passed object.
--
OHM ( Terry Burns ) . . . One-Handed-Man . . .
"Marina" <so*****@nospam.com> wrote in message news:ep**************@TK2MSFTNGP11.phx.gbl... You can either create an object with a bunch of properties - and you set those properties. You can also have ByRef parameters in your function, and return values
by setting those parameters - so they consumer of your functin can check
the values of those parameters after the function is done.
"Nikolay Petrov" <jo******@mail.bg> wrote in message news:e4**************@TK2MSFTNGP11.phx.gbl... How can I return multiple values from a custom function?
TIA
| | |
Thats not quite right. Try it out and you will see that a new object is not
created byVal.
--
OHM ( Terry Burns )
. . . One-Handed-Man . . .
"Marina" <so*****@nospam.com> wrote in message
news:uL**************@TK2MSFTNGP12.phx.gbl... That is true in most cases.
However, consider this:
Dim var as Object SomeFunction(var)
And SomeFunction sets it's parameter to a new instance of an object. In this particular case, if the parameter is declared ByVar, 'var' is still Nothing after the call. If it's ByRef, 'var' will be whatever
SomeFunction set it to.
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in
message news:Oc**************@TK2MSFTNGP10.phx.gbl... ByRef is only relevent for primitives in this respect. Passing a class
to function ByRef or ByVal would not affect the called functions ability
to change the state of the passed object.
--
OHM ( Terry Burns ) . . . One-Handed-Man . . .
"Marina" <so*****@nospam.com> wrote in message news:ep**************@TK2MSFTNGP11.phx.gbl... You can either create an object with a bunch of properties - and you
set those properties. You can also have ByRef parameters in your function, and return values by setting those parameters - so they consumer of your functin can check the values of those parameters after the function is done.
"Nikolay Petrov" <jo******@mail.bg> wrote in message news:e4**************@TK2MSFTNGP11.phx.gbl... > How can I return multiple values from a custom function? > > TIA > >
| | |
Yes, I am aware of that but he doesent says what types he is returning, so
you are assuming that they be mixed.
--
OHM ( Terry Burns )
. . . One-Handed-Man . . .
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl... Terry, As Marina suggests, ByRef is required for both value & reference types, as Nikolay wants to RETURN multiple values.
Dim s As String Dim i As Integer Dim p As Person
CreateStuff(s, i, p)
Public Sub CreateStuff(ByRef sOut As String, _ ByRef iOut As Integer, _ ByRef pOut As Person)
sOut = "Hello World" iOut = 5 pOut = New Person("Me") End Sub
You are correct ByVal is only needed if you are only changing the state of
a passed object.
Hope this helps Jay
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in
message news:Oc**************@TK2MSFTNGP10.phx.gbl... ByRef is only relevent for primitives in this respect. Passing a class
to function ByRef or ByVal would not affect the called functions ability
to change the state of the passed object.
--
OHM ( Terry Burns ) . . . One-Handed-Man . . .
"Marina" <so*****@nospam.com> wrote in message news:ep**************@TK2MSFTNGP11.phx.gbl... You can either create an object with a bunch of properties - and you
set those properties. You can also have ByRef parameters in your function, and return values by setting those parameters - so they consumer of your functin can check the values of those parameters after the function is done.
"Nikolay Petrov" <jo******@mail.bg> wrote in message news:e4**************@TK2MSFTNGP11.phx.gbl... > How can I return multiple values from a custom function? > > TIA > >
| | |
Terry,
Please reread the original question, the OP wants to return multiple values,
you need to use ByRef to return multiple values. It doesn't matter what the
type of the parameter is, you need to use ByRef to return a value through
that parameter!
Remember we are talking returning objects/values, not modifying existing
objects.
As you pointed out, you can change the state of an existing object passed
ByVal.
Hope this helps
Jay
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message
news:eV**************@TK2MSFTNGP10.phx.gbl... Yes, I am aware of that but he doesent says what types he is returning, so you are assuming that they be mixed.
--
OHM ( Terry Burns ) . . . One-Handed-Man . . .
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl... Terry, As Marina suggests, ByRef is required for both value & reference types,
as Nikolay wants to RETURN multiple values.
Dim s As String Dim i As Integer Dim p As Person
CreateStuff(s, i, p)
Public Sub CreateStuff(ByRef sOut As String, _ ByRef iOut As Integer, _ ByRef pOut As Person)
sOut = "Hello World" iOut = 5 pOut = New Person("Me") End Sub
You are correct ByVal is only needed if you are only changing the state
of a passed object.
Hope this helps Jay
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message news:Oc**************@TK2MSFTNGP10.phx.gbl... ByRef is only relevent for primitives in this respect. Passing a class to function ByRef or ByVal would not affect the called functions ability to change the state of the passed object.
--
OHM ( Terry Burns ) . . . One-Handed-Man . . .
"Marina" <so*****@nospam.com> wrote in message news:ep**************@TK2MSFTNGP11.phx.gbl... > You can either create an object with a bunch of properties - and you set > those properties. > You can also have ByRef parameters in your function, and return
values by > setting those parameters - so they consumer of your functin can
check the > values of those parameters after the function is done. > > "Nikolay Petrov" <jo******@mail.bg> wrote in message > news:e4**************@TK2MSFTNGP11.phx.gbl... > > How can I return multiple values from a custom function? > > > > TIA > > > > > >
| | |
That was exactly my point, reread my post.
Using ByVal, 'var' would still be Nothing. Using ByRef, 'var' would be an
object.
Point being, that ByRef makes a difference when talking about non-struct
types (classes), because it allows to 'return' objects in this manner.
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl... Thats not quite right. Try it out and you will see that a new object is
not created byVal.
--
OHM ( Terry Burns ) . . . One-Handed-Man . . .
"Marina" <so*****@nospam.com> wrote in message news:uL**************@TK2MSFTNGP12.phx.gbl... That is true in most cases.
However, consider this:
Dim var as Object SomeFunction(var)
And SomeFunction sets it's parameter to a new instance of an object. In this particular case, if the parameter is declared ByVar, 'var' is still Nothing after the call. If it's ByRef, 'var' will be whatever SomeFunction set it to.
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message news:Oc**************@TK2MSFTNGP10.phx.gbl... ByRef is only relevent for primitives in this respect. Passing a class to function ByRef or ByVal would not affect the called functions ability to change the state of the passed object.
--
OHM ( Terry Burns ) . . . One-Handed-Man . . .
"Marina" <so*****@nospam.com> wrote in message news:ep**************@TK2MSFTNGP11.phx.gbl... > You can either create an object with a bunch of properties - and you set > those properties. > You can also have ByRef parameters in your function, and return
values by > setting those parameters - so they consumer of your functin can
check the > values of those parameters after the function is done. > > "Nikolay Petrov" <jo******@mail.bg> wrote in message > news:e4**************@TK2MSFTNGP11.phx.gbl... > > How can I return multiple values from a custom function? > > > > TIA > > > > > >
| | |
Yes correct, we are talking at cross purposes here.
--
OHM ( Terry Burns )
. . . One-Handed-Man . . .
"Marina" <so*****@nospam.com> wrote in message
news:eq*************@tk2msftngp13.phx.gbl... That was exactly my point, reread my post.
Using ByVal, 'var' would still be Nothing. Using ByRef, 'var' would be an object.
Point being, that ByRef makes a difference when talking about non-struct types (classes), because it allows to 'return' objects in this manner.
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in
message news:%2****************@tk2msftngp13.phx.gbl... Thats not quite right. Try it out and you will see that a new object is not created byVal.
--
OHM ( Terry Burns ) . . . One-Handed-Man . . .
"Marina" <so*****@nospam.com> wrote in message news:uL**************@TK2MSFTNGP12.phx.gbl... That is true in most cases.
However, consider this:
Dim var as Object SomeFunction(var)
And SomeFunction sets it's parameter to a new instance of an object.
In this particular case, if the parameter is declared ByVar, 'var' is
still Nothing after the call. If it's ByRef, 'var' will be whatever SomeFunction set it to.
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message news:Oc**************@TK2MSFTNGP10.phx.gbl... > ByRef is only relevent for primitives in this respect. Passing a
class to > function ByRef or ByVal would not affect the called functions
ability to > change the state of the passed object. > > -- > > OHM ( Terry Burns ) > . . . One-Handed-Man . . . > > > "Marina" <so*****@nospam.com> wrote in message > news:ep**************@TK2MSFTNGP11.phx.gbl... > > You can either create an object with a bunch of properties - and
you set > > those properties. > > You can also have ByRef parameters in your function, and return values by > > setting those parameters - so they consumer of your functin can check the > > values of those parameters after the function is done. > > > > "Nikolay Petrov" <jo******@mail.bg> wrote in message > > news:e4**************@TK2MSFTNGP11.phx.gbl... > > > How can I return multiple values from a custom function? > > > > > > TIA > > > > > > > > > > > >
| | |
Yes, I know, this is a case of mis-communication. When I was referring to
'mixed types', I meant value type and reference types, not differening
'value types'.
We are both singing from the same sheet, just with different expressions.
--
OHM ( Terry Burns )
. . . One-Handed-Man . . .
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:eb**************@tk2msftngp13.phx.gbl... Terry, Please reread the original question, the OP wants to return multiple
values, you need to use ByRef to return multiple values. It doesn't matter what
the type of the parameter is, you need to use ByRef to return a value through that parameter!
Remember we are talking returning objects/values, not modifying existing objects.
As you pointed out, you can change the state of an existing object passed ByVal.
Hope this helps Jay
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in
message news:eV**************@TK2MSFTNGP10.phx.gbl... Yes, I am aware of that but he doesent says what types he is returning,
so you are assuming that they be mixed.
--
OHM ( Terry Burns ) . . . One-Handed-Man . . .
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in
message news:%2****************@TK2MSFTNGP09.phx.gbl... Terry, As Marina suggests, ByRef is required for both value & reference
types, as Nikolay wants to RETURN multiple values.
Dim s As String Dim i As Integer Dim p As Person
CreateStuff(s, i, p)
Public Sub CreateStuff(ByRef sOut As String, _ ByRef iOut As Integer, _ ByRef pOut As Person)
sOut = "Hello World" iOut = 5 pOut = New Person("Me") End Sub
You are correct ByVal is only needed if you are only changing the
state of a passed object.
Hope this helps Jay
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message news:Oc**************@TK2MSFTNGP10.phx.gbl... > ByRef is only relevent for primitives in this respect. Passing a
class to > function ByRef or ByVal would not affect the called functions
ability to > change the state of the passed object. > > -- > > OHM ( Terry Burns ) > . . . One-Handed-Man . . . > > > "Marina" <so*****@nospam.com> wrote in message > news:ep**************@TK2MSFTNGP11.phx.gbl... > > You can either create an object with a bunch of properties - and
you set > > those properties. > > You can also have ByRef parameters in your function, and return values by > > setting those parameters - so they consumer of your functin can check the > > values of those parameters after the function is done. > > > > "Nikolay Petrov" <jo******@mail.bg> wrote in message > > news:e4**************@TK2MSFTNGP11.phx.gbl... > > > How can I return multiple values from a custom function? > > > > > > TIA > > > > > > > > > > > >
| | |
Hi All,
Terry was in my opinion not refering to the message from the OP however to
the answer from Marina about passing an object by reference or by value.
About the values as Marina wrote it, Terry has said nothing in my opinion
and there can be in my opinion no misunderstaning about that.
The first message from Marina was in my opinion complete correct, however
the reference point of the object by ref or by value in the arguing between
OHM and Terry is interesting.
Therefore I was curious and I tried it.
Public Module Main
Public Sub Main()
Dim Jay As Object
MessageBox.Show(Marina(Jay).ToString & " " & Jay.ToString)
MessageBox.Show(Terry(Jay).ToString & " " & Jay.ToString)
End Sub
Private Function Terry(ByVal Cor As Object) As Object
Cor = New Object
Cor = "Hello"
Return Cor
End Function
Private Function Marina(ByRef Cor As Object) As Object
Cor = New Object
Cor = "Hello"
Return Cor
End Function
End Module
This give to times
Hello Hello
Hello Hello
Cor | | |
Haha
Right, that is because Marina is ByRef, so Jay is set right off the bat.
Reversing those function calls, will cause a NullReferenceException, since
Terry is ByVal, so calling Jay.ToString will cause the error.
"Cor Ligthert" <no**********@planet.nl> wrote in message
news:eP**************@TK2MSFTNGP11.phx.gbl... Hi All,
Terry was in my opinion not refering to the message from the OP however to the answer from Marina about passing an object by reference or by value.
About the values as Marina wrote it, Terry has said nothing in my opinion and there can be in my opinion no misunderstaning about that.
The first message from Marina was in my opinion complete correct, however the reference point of the object by ref or by value in the arguing
between OHM and Terry is interesting.
Therefore I was curious and I tried it.
Public Module Main Public Sub Main() Dim Jay As Object MessageBox.Show(Marina(Jay).ToString & " " & Jay.ToString) MessageBox.Show(Terry(Jay).ToString & " " & Jay.ToString) End Sub Private Function Terry(ByVal Cor As Object) As Object Cor = New Object Cor = "Hello" Return Cor End Function Private Function Marina(ByRef Cor As Object) As Object Cor = New Object Cor = "Hello" Return Cor End Function End Module
This give to times Hello Hello Hello Hello
Cor
| | |
Hi Marina,
Right Haha I could not find any explanation for it.
My head is flat from stumping to it now.
Cor Haha
Right, that is because Marina is ByRef, so Jay is set right off the bat.
Reversing those function calls, will cause a NullReferenceException, since Terry is ByVal, so calling Jay.ToString will cause the error.
"Cor Ligthert" <no**********@planet.nl> wrote in message news:eP**************@TK2MSFTNGP11.phx.gbl... Hi All,
Terry was in my opinion not refering to the message from the OP however
to the answer from Marina about passing an object by reference or by value.
About the values as Marina wrote it, Terry has said nothing in my
opinion and there can be in my opinion no misunderstaning about that.
The first message from Marina was in my opinion complete correct,
however the reference point of the object by ref or by value in the arguing between OHM and Terry is interesting.
Therefore I was curious and I tried it.
Public Module Main Public Sub Main() Dim Jay As Object MessageBox.Show(Marina(Jay).ToString & " " & Jay.ToString) MessageBox.Show(Terry(Jay).ToString & " " & Jay.ToString) End Sub Private Function Terry(ByVal Cor As Object) As Object Cor = New Object Cor = "Hello" Return Cor End Function Private Function Marina(ByRef Cor As Object) As Object Cor = New Object Cor = "Hello" Return Cor End Function End Module
This give to times Hello Hello Hello Hello
Cor
| | |
* "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> scripsit: Please reread the original question, the OP wants to return multiple values, you need to use ByRef to return multiple values. It doesn't matter what the type of the parameter is, you need to use ByRef to return a value through that parameter!
Mhm. Isn't setting a property of an object passed into the method
similar to returning a value too? Remember we are talking returning objects/values, not modifying existing objects.
Yep, but assgning something to an object's properties is very close to
returning something.
--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/> | | This discussion thread is closed Replies have been disabled for this discussion. Similar topics
66 posts
views
Thread by Darren Dale |
last post: by
|
5 posts
views
Thread by Oksana Yasynska |
last post: by
|
12 posts
views
Thread by Jose Fernandez |
last post: by
|
5 posts
views
Thread by D. Shane Fowlkes |
last post: by
|
14 posts
views
Thread by dcassar |
last post: by
|
8 posts
views
Thread by aleksandar.ristovski |
last post: by
|
80 posts
views
Thread by xicloid |
last post: by
| |
4 posts
views
Thread by Jonathan |
last post: by
| | | | | | | | | | |