Reference to a variable

R

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.
 
A

Armin Zingler

Rob Nicholson said:
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
 
R

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.
 
A

Armin Zingler

Rob Nicholson said:
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
 
R

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.
 
A

Armin Zingler

Rob Nicholson said:
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
 
T

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

--------------------
 

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