Private Sub myProcedure (byref obj as Object)
'now you have a reference to myObject in your sub that you can use for
whatever. If you change obj to reference a different object then myObjec in
your calling module/class will also be changed to the new object.
...
end sub
--
Dennis in Houston
"arthernan@hotm ail.com" wrote:
[color=blue]
>
> Say I pass an object ByRef. And I want to store a reference to that
> object. How do I do this?
>
>
> Arturo Hernandez
>
>[/color]
Arturo,
[color=blue]
> Say I pass an object ByRef. And I want to store a reference to that
> object. How do I do this?
>[/color]
I am not sure if I understand your answer, however I assume that you want to
pass an object by value (a copy of the reference of the object is than
passed as the value).
<arthernan@hotm ail.com> schrieb:[color=blue]
> Say I pass an object ByRef. And I want to store a reference to that
> object. How do I do this?[/color]
There are two kinds of types in .NET: Reference types and value types.
Objects which are instances of a value type are always copied when being
assigned to another variable whereas assigning a reference to an instance of
a reference type will create a new reference. A more detailed explanation
of what's going on when passing objects to a method can be found here:
"Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
news:OPHmg3kyFH A.3588@tk2msftn gp13.phx.gbl...[color=blue]
> <arthernan@hotm ail.com> schrieb:[color=green]
>> Say I pass an object ByRef. And I want to store a reference to that
>> object. How do I do this?[/color]
>
> There are two kinds of types in .NET: Reference types and value types.
> Objects which are instances of a value type are always copied when being
> assigned to another variable whereas assigning a reference to an instance
> of a reference type will create a new reference. A more detailed
> explanation of what's going on when passing objects to a method can be
> found here:[/color]
Dont you mean that Objects which are instances of a value type which are
passed by value are copies of the original object and therefore when values
are assigned TO them it is to the copy not the original.
In your text above, it appears as if you are saying that these objects which
are passed by value are copied before the value can be assigned, which of
course would seem to be irrelevant because if you only assigned a passed
objects value then there would be no need to copy it.
Make sense ??
[color=blue]
>
> <URL:http://groups.google.d e/group/microsoft.publi c.dotnet.langua ges.vb/msg/25064a4fedd3663 3>
>
> --
> M S Herfried K. Wagner
> M V P <URL:http://dotnet.mvps.org/>
> V B <URL:http://classicvb.org/petition/>[/color]
"Mr Newbie" <here@now.com > schrieb:[color=blue][color=green][color=darkred]
>>> Say I pass an object ByRef. And I want to store a reference to that
>>> object. How do I do this?[/color]
>>
>> There are two kinds of types in .NET: Reference types and value types.
>> Objects which are instances of a value type are always copied when being
>> assigned to another variable whereas assigning a reference to an instance
>> of a reference type will create a new reference. A more detailed
>> explanation of what's going on when passing objects to a method can be
>> found here:[/color]
>
> Dont you mean that Objects which are instances of a value type which are
> passed by value are copies of the original object and therefore when
> values > are assigned TO them it is to the copy not the original.[/color]
You are right -- I was referrint to variable assignment, not passing the
variable in a method's parameter.
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Herfried K. Wagner [MVP] wrote:[color=blue]
> <arthernan@hotm ail.com> schrieb:[color=green]
> > Say I pass an object ByRef. And I want to store a reference to that
> > object. How do I do this?[/color]
>
> There are two kinds of types in .NET: Reference types and value types.
> Objects which are instances of a value type are always copied when being
> assigned to another variable whereas assigning a reference to an instance of
> a reference type will create a new reference. A more detailed explanation
> of what's going on when passing objects to a method can be found here:
>
> <URL:http://groups.google.d e/group/microsoft.publi c.dotnet.langua ges.vb/msg/25064a4fedd3663 3>
>[/color]
This is an example:
Class HumpaLumpa
Public Sub CoolSub(ByRef pCar As Car)
Me.vCar=pCar
End Sub
<arthernan@hotm ail.com> schrieb:[color=blue]
> Class HumpaLumpa
>
> Public Sub CoolSub(ByRef pCar As Car)
> Me.vCar=pCar
> End Sub
> .
> .
> .
> Me.vCar.WindowD own=True 'did the original object get changed or
> just a copy of it?[/color]
The original object because 'Car' is a class and thus a reference type.
[color=blue]
> This is a second example:
>
> Class HumpaLumpa
>
> Public Sub CoolSub(ByRef pCar As Integer)
> Me.vCar=pCar
> End Sub
> .
> .
> .
> Me.vCar=22 'did the original variable get changed or
> just a copy of it?[/color]
Just a copy of it. The assignment 'Me.vCar = pCar' will create a copy of
'pCar''s value and assign it to 'vCar'.
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
But, the end result is that the vChar property will be changed in the
original object.
Object references are just that. They only point to the actual object. If
you pass an object reference ByRef it still is just a pointer to the object.
The only difference is that the original object reference will change if you
assign the passed reference. For example:
Private sub Foo
Dim Bar as New Bar
SetFoo(Bar)
'When you come back here Bar now points to the new object created in SetFoo
End Sub
Private Sub SetFoo(byRef Bar as Bar)
Bar.Foo = "New Value" ' Changes the property in the original object
created in Foo
Bar = New Bar ' Creates a new object
End Sub
If parameter Bar in the SetFoo sub was defined as ByVal then the original
Bar variable in Sub Foo will not change and still point to the original
object.
NOTE: This is horrible programming but does show the difference on object
references passed as ByRef or ByVal.
"Herfried K. Wagner [MVP]" wrote:
[color=blue]
> <arthernan@hotm ail.com> schrieb:[color=green]
> > Class HumpaLumpa
> >
> > Public Sub CoolSub(ByRef pCar As Car)
> > Me.vCar=pCar
> > End Sub
> > .
> > .
> > .
> > Me.vCar.WindowD own=True 'did the original object get changed or
> > just a copy of it?[/color]
>
> The original object because 'Car' is a class and thus a reference type.
>[color=green]
> > This is a second example:
> >
> > Class HumpaLumpa
> >
> > Public Sub CoolSub(ByRef pCar As Integer)
> > Me.vCar=pCar
> > End Sub
> > .
> > .
> > .
> > Me.vCar=22 'did the original variable get changed or
> > just a copy of it?[/color]
>
> Just a copy of it. The assignment 'Me.vCar = pCar' will create a copy of
> 'pCar''s value and assign it to 'vCar'.
>
> --
> M S Herfried K. Wagner
> M V P <URL:http://dotnet.mvps.org/>
> V B <URL:http://classicvb.org/petition/>
>
>[/color]
>Just a copy of it. The assignment 'Me.vCar = pCar' will create a copy of[color=blue]
>'pCar''s value and assign it to 'vCar'.
>
>--
> M S Herfried K. Wagner
>M V P <URL:http://dotnet.mvps.org/>
> V B <URL:http://classicvb.org/petition/>[/color]
Thank you, this is a little bit confusing aspect of VB. But I think I
got it.
Comment