Link to home
Start Free TrialLog in
Avatar of dkim18
dkim18

asked on

output parameter vs ByRef in vb.net?

Hi,

When to use the output parameter? And ByRef?
Doesn't function already return an output?
How to return multiple outputs?

Function CalculateMax(ByVal input as Integer, ByRef output as Integer) as Integer

output = input
max = input
Return max
End Function
SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Not quite right CodeCruiser...

See "Differences Between Passing an Argument By Value and By Reference (Visual Basic)":
http://msdn.microsoft.com/en-us/library/eek064h4.aspx

    "The value of a reference type is a pointer to the data elsewhere in memory. This means that when you pass a reference type by value, the procedure code has a pointer to the underlying element's data, even though it cannot access the underlying element itself."

Translation: Reference types passed with ByVal can modify the members, but not the memory location that the variable points to.

In other words, Reference types passed with ByRef, can actually be REPLACED WITHIN the sub/function with a NEW instance, and the original variable OUTSIDE that sub/function will now also point to the new instance.

If you attempt the above with a Reference type passed ByVal, then the original variable outside the sub/function will still point to the original instance without being changed.  The new instance pointed to by the local variable in the sub/function is simply discarded.
Thanks for clarifying Idle_Mind.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Excellent example sir...