Return Multiple Values from a Custom Function

  • Thread starter Thread starter Nikolay Petrov
  • Start date Start date
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.
 
Return a Structure

Friend MyFunction() as MyStructure

Dim ms as MyStructure

ms.<properties>= . . . .

Return MyStructure

End Function
 
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.
 
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.
 
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
 
Thats not quite right. Try it out and you will see that a new object is not
created byVal.
 
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 . . .
 
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
 
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.
 
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 . . .
 
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.
 
Hi Marina,

Right Haha I could not find any explanation for it.
My head is flat from stumping to it now.

Cor
 
* "Jay B. Harlow said:
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.
 
Back
Top