How can I return multiple values from a custom function?
TIA 16 17292
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 thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Darren Dale |
last post by:
Hello,
def test(data):
i = ? This is the line I have trouble with
if i==1: return data
else: return data
a,b,c,d = test()
|
by: Oksana Yasynska |
last post by:
Hi all,
I'm running Postgres 7.2.1 and I need to return multiple row sets from plpgsql
function. I'm new in plpgsql but according documentation and everything I
could find in the mailing list...
|
by: Jose Fernandez |
last post by:
Hello.
I'm building a web service and I get this error.
NEWS.News.CoverNews(string)': not all code paths return a value
This is the WebMethod
public SqlDataReader CoverNews(string Sport)...
|
by: D. Shane Fowlkes |
last post by:
This may be a very basic question but it's something I've never done before.
I've looked at a couple of my favorite sites and books and can't find an
answer either. I can write a Function to...
|
by: dcassar |
last post by:
I have had a lively discussion with some coworkers and decided to get
some general feedback on an issue that I could find very little
guidance on. Why is it considered bad practice to define a...
|
by: aleksandar.ristovski |
last post by:
Hello all,
I have been thinking about a possible extension to C/C++ syntax.
The current syntax allows declaring a function that returns a value:
int foo();
however, if I were to return...
|
by: xicloid |
last post by:
I'm making a function that checks the input integer and returns the
value if it is a prime number.
If the integer is not a prime number, then the function should return
nothing.
Problem is, I...
|
by: ADezii |
last post by:
The incentive for this Tip was an Article by the amazing Allen Browne - I considered it noteworthy enough to post as The Tip of the Week in this Access Forum.
Original Article by Allen Browne
...
|
by: Jonathan |
last post by:
I have a SQL stored procedure for adding a new record in a transactions
table. It also has two return values: CounterID and IDKey.
I want to create a webservice that accepts the 10 input...
|
by: linyimin |
last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
|
by: erikbower65 |
last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA:
1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
|
by: kcodez |
last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: DJRhino1175 |
last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this -
If...
|
by: DJRhino |
last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer)
If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _
310030356 Or 310030359 Or 310030362 Or...
|
by: lllomh |
last post by:
How does React native implement an English player?
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
| |