Return ByRef

  • Thread starter Thread starter Samuel Shulman
  • Start date Start date
S

Samuel Shulman

Hi

I wander there is a way to return ByRef just like passing ByRef

What I want to achieve is the following:

Call a method that returns an object

Use that call as an argument to a ByRef parameter then on the return I want
to access that Object directly that has a global scope

Currently the Original Object doesn't seem to pass and therefore it has no
value

Thank you,
Sam
 
What you are proposing is illogical. Even if you could code something like:

Public Sub A(ByRef B As Object)

B = 2

End Sub

Public Function C() As Object

Return 1

End Sub

Call A(C())

Console.WriteLine(C())

the result would always be 1 because that is the the result of the most
recent call to C().

However, it would fail because the call to A(C()) would attempt to update
the result of C() which, by definition, is read only.

To achieve what you want, you must code the calls as:

Dim D As Object = C()

Call A(D)

Console.WriteLine(D)

the result of which would be 2, i.e. the return value from C() assigned to D
which is, in turn, modified by the call to A(D).

The scope of D can be anything you want and you may very well declare it so
that it is 'global' (as you put it).
 
Thank you for your reply,

Your description is not what I tried to describe

I my case I never overwrite that variable but I thought that since what
returns is a class type then the value is only a reference and therefore it
point to the same global variable so I would expect the value to remain


Samuel
 
Samuel,
Can you better describe what you are attempting?

It almost sounds like you are thinking C++ or you are simply confusing
reference types with ByRef parameters.

Within .NET: The Class keyword defines a Reference type, this means that the
object exists on the heap: return values, fields, variables & parameters
refer (reference) to this object on the heap.
While the "Structure" keyword defines a Value type. This means that the
"object" (value really) exists "in-line" locally either on the stack in the
case of return values, variables & parameters or inside a larger object on
the heap. Boxed value types are special in that the value exists as an
object on the heap.

Return values will simply return the value of a Value type (Structure) or
the reference to the object on the heap of a Reference Type (Class,
Interface, Delegate).


Again can you better describe what you are attempting, or at the very least
include pseudo code on what you are expecting.
Call a method that returns an object
Public Function Something() As Object
End Function
Use that call as an argument to a ByRef parameter then
Public Sub SomethingElse(ByRef value As Object)
End Sub

SomethingElse(Something())
on the return I want to access that Object directly that has a global
scope
Huh? that object was passed to the SomethingElse method, you didn't assign
it to "global scope" anything...
 
Samuel,

Samuel Shulman said:
I wander there is a way to return ByRef just like passing ByRef

What I want to achieve is the following:

Call a method that returns an object

Use that call as an argument to a ByRef parameter then on the return I
want to access that Object directly that has a global scope

Currently the Original Object doesn't seem to pass and therefore it has no
value

Something like this (pseudo code)?

\\\
Private Function Sub A(ByRef o As Object) As Object
Return ByRef o ' !
End Sub

Private Function B(ByRef o As Object) As Object
o = New Foo()
End Sub

Private Sub Test()
Dim o As Object
B(A(o))

' 'o' points to an instance of 'Foo' here.
End Sub
///
 
Samuel,

If you pass ByVal a referencetype (what we often call a class/object) than
you don't have to return byRef.

The used object is already changed and in fact can you use the reference.

With value types you are always passing by value so this is impossible.
(Or you should first put them in an object)

Cor
 
Thank you for the reply,


Public A as SqlException 'Global varibale

Public Function Method as SqlException
Return A
End Function

Public sub Method2 (ByRef ob as SqlException)
Try
Throw new SqlException
Catch Ex AS SQL
ob=Ex
End Sub

Sub Main()
Method2(Method1)

'At this point A = nothing if though a value was assigned to it in method2
End Sub


Samuel
 
Hi Samuel.
Public A as SqlException 'Global varibale

Public Function Method as SqlException
Return A
End Function

Public sub Method2 (ByRef ob as SqlException)
Try
Throw new SqlException
Catch Ex AS SQL
ob=Ex
End Sub

Sub Main()
Method2(Method1)

'At this point A = nothing if though a value was assigned to it in
method2
End Sub

Yes ... it is nothing and it must be nothing.
At the point "Method2(Method1)" you give a result of a method into another
method. This is a expression and not a variable, so the value can't be given
by reference.

Try this instead and all should do fine:

a = method1
Method2(a)
 
thank you

samuel
Torsten Kerz said:
Hi Samuel.


Yes ... it is nothing and it must be nothing.
At the point "Method2(Method1)" you give a result of a method into another
method. This is a expression and not a variable, so the value can't be
given by reference.

Try this instead and all should do fine:

a = method1
Method2(a)
 
Back
Top