By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,033 Members | 1,290 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,033 IT Pros & Developers. It's quick & easy.

Reference to a variable

P: n/a
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.


Feb 16 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"Rob Nicholson" <in******@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

Feb 16 '06 #2

P: n/a
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.
Feb 17 '06 #3

P: n/a
"Rob Nicholson" <in******@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
Feb 17 '06 #4

P: n/a
> 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.
Feb 17 '06 #5

P: n/a
"Rob Nicholson" <in******@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
Feb 17 '06 #6

P: n/a
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

--------------------
From: "Rob Nicholson" <in******@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: <ue**************@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!TK2MSFT NGP14.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 whatI 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.



Feb 20 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.