?
Solved

output parameter vs ByRef in vb.net?

Posted on 2012-08-21
6
Medium Priority
?
596 Views
Last Modified: 2012-08-31
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
0
Comment
Question by:dkim18
  • 3
  • 2
6 Comments
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 664 total points
ID: 38317810
If you want to NOT use ByRef, but return multiple items, then return something that can contain multiple values such as Arrays and Lists, or a CUSTOM CLASS containing multiple members.
0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 668 total points
ID: 38317886
ByRef only applies to value types by the way. Reference types are passed as ByRef anyway.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 38317937
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.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38317974
Thanks for clarifying Idle_Mind.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 668 total points
ID: 38318076
Take the family of *TryParse methods as an example. We use the boolean result as a convenient test as to whether or not the conversion was successful. If we succeeded in converting the value, then the ByRef parameter will hold the converted value. This is often cleaner than doing something like a straight *Parse, and catching the exception if the conversion fails. Purists will decry ByRef parameters as not good.

The same behavior can be achieved using Idle_Mind's suggestion of returning a class/struct with all the appropriate data--there is just another step in confirming the result (i.e. the method call and the evaluation of the result occur in two different lines).
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 38318086
Excellent example sir...
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Loops Section Overview
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question