Return by references or value

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Will the dataset below be returned by value or reference?
Public Shared Function getDS() As DataSet
Dim ds As New DataSet
'... do something
Return ds
End Function
 
Arne,
The "reference" to the data set, will be returned "by value". In other words
a copy of the reference to a single object on the heap will be returned.

DataSet is a Reference Type so variables of type DataSet always hold a
reference to the actual DataSet object. A single copy of the actual DataSet
object exists on the heap.

Where as Integer is a Value Type, so variables of type Integer always hold
the value of the actual Integer 'value'. Multiple copies of the Integer
value exists in each variable (either on the stack or in a Reference Type on
the heap).

Remember ByVal & ByRef are how parameters are passed, Reference Type & Value
Type are how variables are stored. If you pass a Reference Type ByVal a copy
of the reference to a single object on the heap is passed. If you pass a
Value Type ByVal a copy of the 'value' is passed. If you pass a Reference
Type ByRef, then a reference to the original variable is passed, the
original variable still contains a single reference to the object on the
heap. Likewise if you pass a Value Type ByRef, then a reference to the
original variable is passed.

Which allows the called routine of ByRef parameters to modify the what the
original variable contains/references...

Hope this helps
Jay


| Will the dataset below be returned by value or reference?
| Public Shared Function getDS() As DataSet
| Dim ds As New DataSet
| '... do something
| Return ds
| End Function
 
What would by reference, or by value mean in this context? This sort of idea
just doesn't apply when returning variables. How would that work?

That only applies to parameters of methods.

There is only one way you can return an object from a method. And that is to
return a reference to that object. But the word 'reference' here is not
related to the concept of passing parameters 'by reference'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top