Home All Groups Group Topic Archive Search About
Author
19 Dec 2006 8:25 PM
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

Author
19 Dec 2006 8:44 PM
Stephany Young
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).


Show quoteHide quote
"Samuel Shulman" <samuel.shul***@ntlworld.com> wrote in message
news:uJQTMv6IHHA.1816@TK2MSFTNGP06.phx.gbl...
> 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
>
Author
19 Dec 2006 9:32 PM
Samuel Shulman
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



Show quoteHide quote
"Stephany Young" <noone@localhost> wrote in message
news:%23EB%23F66IHHA.4068@TK2MSFTNGP03.phx.gbl...
> 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).
>
>
> "Samuel Shulman" <samuel.shul***@ntlworld.com> wrote in message
> news:uJQTMv6IHHA.1816@TK2MSFTNGP06.phx.gbl...
>> 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
>>
>
>
Author
19 Dec 2006 9:40 PM
Stephany Young
In that case I have absolutely no idea what you were attempting to describe.


Show quoteHide quote
"Samuel Shulman" <samuel.shul***@ntlworld.com> wrote in message
news:uwsFuU7IHHA.2632@TK2MSFTNGP06.phx.gbl...
> 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
>
>
>
> "Stephany Young" <noone@localhost> wrote in message
> news:%23EB%23F66IHHA.4068@TK2MSFTNGP03.phx.gbl...
>> 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).
>>
>>
>> "Samuel Shulman" <samuel.shul***@ntlworld.com> wrote in message
>> news:uJQTMv6IHHA.1816@TK2MSFTNGP06.phx.gbl...
>>> 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
>>>
>>
>>
>
>
Author
19 Dec 2006 11:20 PM
Jay B. Harlow
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...

--
Hope this helps
Jay B. Harlow
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


Show quoteHide quote
"Samuel Shulman" <samuel.shul***@ntlworld.com> wrote in message
news:uJQTMv6IHHA.1816@TK2MSFTNGP06.phx.gbl...
> 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
>
Author
20 Dec 2006 1:00 PM
Samuel Shulman
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


Show quoteHide quote
"Jay B. Harlow" <Jay_Harlow_***@tsbradley.net> wrote in message
news:%238bSDR8IHHA.816@TK2MSFTNGP06.phx.gbl...
> 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...
>
> --
> Hope this helps
> Jay B. Harlow
> .NET Application Architect, Enthusiast, & Evangelist
> T.S. Bradley - http://www.tsbradley.net
>
>
> "Samuel Shulman" <samuel.shul***@ntlworld.com> wrote in message
> news:uJQTMv6IHHA.1816@TK2MSFTNGP06.phx.gbl...
>> 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
>>
>
Author
20 Dec 2006 1:43 PM
Torsten Kerz
Hi Samuel.

Show quoteHide quote
> 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)


Show quoteHide quote
>
>
> "Jay B. Harlow" <Jay_Harlow_***@tsbradley.net> wrote in message
> news:%238bSDR8IHHA.816@TK2MSFTNGP06.phx.gbl...
>> 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...
>>
>> --
>> Hope this helps
>> Jay B. Harlow
>> .NET Application Architect, Enthusiast, & Evangelist
>> T.S. Bradley - http://www.tsbradley.net
>>
>>
>> "Samuel Shulman" <samuel.shul***@ntlworld.com> wrote in message
>> news:uJQTMv6IHHA.1816@TK2MSFTNGP06.phx.gbl...
>>> 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
>>>
>>
>
>
Author
20 Dec 2006 1:54 PM
Samuel Shulman
thank you

samuel
Show quoteHide quote
"Torsten Kerz" <tk***@freenet.de> wrote in message
news:%23MwAhzDJHHA.4112@TK2MSFTNGP04.phx.gbl...
> 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)
>
>
>>
>>
>> "Jay B. Harlow" <Jay_Harlow_***@tsbradley.net> wrote in message
>> news:%238bSDR8IHHA.816@TK2MSFTNGP06.phx.gbl...
>>> 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...
>>>
>>> --
>>> Hope this helps
>>> Jay B. Harlow
>>> .NET Application Architect, Enthusiast, & Evangelist
>>> T.S. Bradley - http://www.tsbradley.net
>>>
>>>
>>> "Samuel Shulman" <samuel.shul***@ntlworld.com> wrote in message
>>> news:uJQTMv6IHHA.1816@TK2MSFTNGP06.phx.gbl...
>>>> 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
>>>>
>>>
>>
>>
>
>
Author
20 Dec 2006 1:49 AM
Herfried K. Wagner [MVP]
Samuel,

"Samuel Shulman" <samuel.shul***@ntlworld.com> schrieb:
> 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
///

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>
Author
20 Dec 2006 4:58 AM
Cor Ligthert [MVP]
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