References to references in VB .NET

G

Guest

I have 2 windows forms. In the first form I have a reference to an object. I
want to assign an object to that reference from the second form. If I pass
this reference to the new(...) method of the second form ByRef, I can easily
assign an object to the reference in the first form, as long as I do it in
the new(...) method.

However, I don't want to make this assignment in the constructor of the
second form. So I thought that I would create a second reference(of the same
type as the first) in the second form and assign the reference passed from
the first form to this reference in the second form. I would then try to
assign an object to the reference in the second form, hoping that it would in
effect be changing what the reference in the first form was pointing to. (In
my C days this was getting a pointer to a pointer).

But it doesn't work as expected. I can't make the reference from the first
form point to an object that I've assigned to the reference in the second
form, even though I assigned one reference to the other in the new(...)
method, which took the first reference ByRef in the constructors parameter
list.

Is there a way in VB .NET to create a reference to a reference so that I can
delay assignment of an object to a reference passed ByRef from the new(...)
to a different place in the second form?

Michael
 
M

Mohamoss

Hi Michael
This is because what you have here is to references not just one .when
you pass the ref parameter to the second form and assign it to a new
reference . you now get 2 references pointing to the same object . So ,when
you change the second one ( that of form two ) to point to new object ,
that of course will not make the first reference ( of from one ) point to
that new object and it will still point to the old one . In c and C++
we can explicitly declare pointers and we can declare even declare
pointers to them ** , which would have solved the problem of course .
however , here we can only play with references when passing parameters .
so you can either pass this reference back to from one ( so it would be
changes ) . or you can just override the values of the object pointed to by
first reference by the values of the second object that is pointed to by
the new ref. this way you will have same values gotten by both references,
however still they are to different objects ( just holding the same value
. Hope this helps

Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC
 
G

Guest

So, unlike C/C++, in VB .NET, I can't create a reference to a reference. I
can pass an object Byref so that I can manipulate the original object.
However, I can't pass a reference to that object and manipulate that
reference so that it ends up pointing to another object.

Wonder VB leaves out this ability.

Thanks for your help

Michael
 

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

Top