Do Object references exist in VB????

  • Thread starter Thread starter arthernan
  • Start date Start date
A

arthernan

Say I pass an object ByRef. And I want to store a reference to that
object. How do I do this?


Arturo Hernandez
 
dim myObject as Object

myProcedure(myObject)


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
 
Arturo,
Say I pass an object ByRef. And I want to store a reference to that
object. How do I do this?
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).

I hope this helps,

Cor
 
Say I pass an object ByRef. And I want to store a reference to that
object. How do I do this?

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.de/group/microsoft.public.dotnet.languages.vb/msg/25064a4fedd36633>
 
Herfried K. Wagner said:
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:

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 ??
 
Mr Newbie said:
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.

You are right -- I was referrint to variable assignment, not passing the
variable in a method's parameter.
 
Herfried said:
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.de/group/microsoft.public.dotnet.languages.vb/msg/25064a4fedd36633>

This is an example:

Class HumpaLumpa

Public Sub CoolSub(ByRef pCar As Car)
Me.vCar=pCar
End Sub
 
Class HumpaLumpa

Public Sub CoolSub(ByRef pCar As Car)
Me.vCar=pCar
End Sub
.
.
.
Me.vCar.WindowDown=True 'did the original object get changed or
just a copy of it?

The original object because 'Car' is a class and thus a reference type.
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?

Just a copy of it. The assignment 'Me.vCar = pCar' will create a copy of
'pCar''s value and assign it to 'vCar'.
 
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.
 
Just a copy of it. The assignment 'Me.vCar = pCar' will create a copy of
'pCar''s value and assign it to 'vCar'.

Thank you, this is a little bit confusing aspect of VB. But I think I
got it.
 

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