Home All Groups Group Topic Archive Search About

Reference to a variable

Author
16 Feb 2006 6:09 PM
Rob Nicholson
How do you handle references to a variable in VB.NET? Consider something
like this:

Public Class MyClass

    Private m_Widget As MyWidget

End Class

I realise that if you do something like:

m_Widget = New MyWidget

The m_Widget holds a reference to the new MyWidget object. If you then do
something like:

Dim m_Reference As MyWidget = m_Widget

Then m_Reference now holds a reference to the same MyWidget object.

However, what I want is the equivilent of the C++ ** mechanism whereby
m_Reference is actually a reference to the m_Width reference if you see what
I mean :-) So that if the m_Width variable is changed to another object,
then the m_Reference also changes.

In C++ (which I'm real rusty on), I would have done something like:

    Widget *m_Widget, **m_Reference;
    m_Widget = New Widget;
    m_Reference = &m_Widget;

Sorry if that's a real mixture - ages since I wrote a line of C++! And I
know I'm mixing pointers and references.

Thanks, Rob.

Author
16 Feb 2006 6:26 PM
Armin Zingler
Show quote Hide quote
"Rob Nicholson" <informed@community.nospam> schrieb
> How do you handle references to a variable in VB.NET? Consider
> something like this:
>
> Public Class MyClass
>
>    Private m_Widget As MyWidget
>
> End Class
>
> I realise that if you do something like:
>
> m_Widget = New MyWidget
>
> The m_Widget holds a reference to the new MyWidget object. If you
> then do something like:
>
> Dim m_Reference As MyWidget = m_Widget
>
> Then m_Reference now holds a reference to the same MyWidget object.
>
> However, what I want is the equivilent of the C++ ** mechanism
> whereby m_Reference is actually a reference to the m_Width reference
> if you see what I mean :-) So that if the m_Width variable is
> changed to another object, then the m_Reference also changes.
>
> In C++ (which I'm real rusty on), I would have done something like:
>
>    Widget *m_Widget, **m_Reference;
>    m_Widget = New Widget;
>    m_Reference = &m_Widget;
>
> Sorry if that's a real mixture - ages since I wrote a line of C++!
> And I know I'm mixing pointers and references.


If you need a reference to a reference, put the 2nd reference into a class
and store a reference to an instance of that class.


Armin
Author
17 Feb 2006 2:04 PM
Rob Nicholson
> If you need a reference to a reference, put the 2nd reference into a class
> and store a reference to an instance of that class.

Hmm, I thought of that using ByRef in a class method and then storing that
in a class variable:

Public Class RefClass

Private m_Reference as MyWidget

Sub StoreReference(ByRef Widget As MyWidget)
m_Reference = Widget
End Sub

But whilst this does indeed pass in a reference, what gets store in
m_Reference is the reference to the Widget object, not the reference to the
variable referencing the Widget object.

Cheers, Rob.
Author
17 Feb 2006 4:49 PM
Armin Zingler
Show quote Hide quote
"Rob Nicholson" <informed@community.nospam> schrieb
>
> > If you need a reference to a reference, put the 2nd reference into
> > a class and store a reference to an instance of that class.
>
> Hmm, I thought of that using ByRef in a class method and then
> storing that in a class variable:
>
> Public Class RefClass
>
> Private m_Reference as MyWidget
>
> Sub StoreReference(ByRef Widget As MyWidget)
> m_Reference = Widget
> End Sub
>
> But whilst this does indeed pass in a reference, what gets store in
> m_Reference is the reference to the Widget object, not the reference
> to the variable referencing the Widget object.

Correct.
(there's nothing I can add)


Armin
Author
17 Feb 2006 5:49 PM
Rob Nicholson
> Correct.
> (there's nothing I can add)

Hmm, bit of an omission that. I can code around it by in effect creating a
little class that contains the pointer and do it that way. Or was that what
you were suggesting in the first place?

Thanks, Rob.
Author
17 Feb 2006 5:54 PM
Armin Zingler
"Rob Nicholson" <informed@community.nospam> schrieb
> > Correct.
> > (there's nothing I can add)
>
> Hmm, bit of an omission that. I can code around it by in effect
> creating a little class that contains the pointer and do it that
> way. Or was that what you were suggesting in the first place?

Yes, that's what I suggested. But, I've never needed it.


Armin
Author
20 Feb 2006 6:08 AM
TerryFei
Hi Rob,
In addition to Armin's comment, I hope the following information is also
helpful for you.
Based on my knowledge, there is a way which is called "RefByRef", just like:

shared sub Main
    dim myObject as new SomeClass
    Test(myObject)
End sub

Public sub Test(ByRef v1 as SomeClass)
//now v1 has a reference to MyObject(reference) and not to the actual Object
End sub

I hope the above information is helpful for you. Thanks and have a nice day!

Best Regards,

Terry Fei [MSFT]
Microsoft Community Support
Get Secure! www.microsoft.com/security

--------------------
Show quoteHide quote
>From: "Rob Nicholson" <informed@community.nospam>
>Subject: Reference to a variable
>Date: Thu, 16 Feb 2006 18:09:55 -0000
>Lines: 39
>X-Priority: 3
>X-MSMail-Priority: Normal
>X-Newsreader: Microsoft Outlook Express 6.00.3790.1830
>X-RFC2646: Format=Flowed; Original
>X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.1830
>Message-ID: <ueYxbQyMGHA.3***@TK2MSFTNGP14.phx.gbl>
>Newsgroups: microsoft.public.dotnet.languages.vb
>NNTP-Posting-Host: host217-45-2-100.in-addr.btopenworld.com 217.45.2.100
>Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP14.phx.gbl
>Xref: TK2MSFTNGXA01.phx.gbl microsoft.public.dotnet.languages.vb:318033
>X-Tomcat-NG: microsoft.public.dotnet.languages.vb
>
>How do you handle references to a variable in VB.NET? Consider something
>like this:
>
>Public Class MyClass
>
>    Private m_Widget As MyWidget
>
>End Class
>
>I realise that if you do something like:
>
>m_Widget = New MyWidget
>
>The m_Widget holds a reference to the new MyWidget object. If you then do
>something like:
>
>Dim m_Reference As MyWidget = m_Widget
>
>Then m_Reference now holds a reference to the same MyWidget object.
>
>However, what I want is the equivilent of the C++ ** mechanism whereby
>m_Reference is actually a reference to the m_Width reference if you see
what
>I mean :-) So that if the m_Width variable is changed to another object,
>then the m_Reference also changes.
>
>In C++ (which I'm real rusty on), I would have done something like:
>
>    Widget *m_Widget, **m_Reference;
>    m_Widget = New Widget;
>    m_Reference = &m_Widget;
>
>Sorry if that's a real mixture - ages since I wrote a line of C++! And I
>know I'm mixing pointers and references.
>
>Thanks, Rob.
>
>
>
>
>