Re: Return ByRef

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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_MVP@xxxxxxxxxxxxx> wrote in message
news:%238bSDR8IHHA.816@xxxxxxxxxxxxxxxxxxxxxxx
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.shulman@xxxxxxxxxxxx> wrote in message
news:uJQTMv6IHHA.1816@xxxxxxxxxxxxxxxxxxxxxxx
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






.



Relevant Pages

  • Re: Return ByRef
    ... Public Function Method as SqlException ... Public sub Method2 (ByRef ob as SqlException) ... or the reference to the object on the heap of a Reference Type (Class, ... Public Sub SomethingElse ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Casting?
    ... ByRef indicates that you intend on modifying the caller's variable. ... references (you dereference the parameter, ... proven to be a performance problem via profiling (CLR Profiler is one ... >> A Reference Type is an object that exists on the heap. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Property list
    ... > my lenght for propertyinfo always comes up zero? ... Note ArrayList is a Reference type, passing it as ByRef is redundent. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Return ByRef
    ... Public Function Method as SqlException ... Public sub Method2 (ByRef ob as SqlException) ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Why ByRef default changed to ByVal?
    ... ByRef - Reference Type ... exists on the heap. ... Remember Reference types hold a reference to the object, so passing ...
    (microsoft.public.dotnet.languages.vb)