How can I return multiple values from a custom function?
TIA 16 17358
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: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |