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

Nested class: accessing containing class's variables

P: n/a
I'm a little confused here. If I have the following:

Public ClassA
Friend varA As Integer
Private varB As Integer

Private ClassB
Public Sub MethodA()
' How can I access varA and varB here?
End Sub
End Class

End Class

is there anyway for the nested class "ClassB" to access the private or
friend variables contained in "ClassA"? Some sort of identifier? Or do I
need to pass a reference to ClassA in ClassB's constructor (or whereever)?

Thanks,
Joel Moore
Nov 21 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a

"Joel Moore" <as*******@asdaadad.com> wrote in message
news:Xn******************************@207.46.248.1 6...
I'm a little confused here. If I have the following:

Public ClassA
Friend varA As Integer
Private varB As Integer

Private ClassB
Public Sub MethodA()
' How can I access varA and varB here?
End Sub
End Class

End Class

is there anyway for the nested class "ClassB" to access the private or
friend variables contained in "ClassA"? Some sort of identifier? Or do I
need to pass a reference to ClassA in ClassB's constructor (or whereever)?


Yes, the nested class has access to all the members of the enclosing class
(or so the docs say) but I didn't see an example of how to access the
variables. I'll look it up when I get home.

As a guess, I'm sure you can just refer to the members by name. I'm
particularly interested in situations where you define other members with
the same names in the nested class and then you want to refer to the member
in the enclosing class. Perhaps the best advice in that situation is "don't
set it up that way," but let's see....
Nov 21 '05 #2

P: n/a
Joel,
Or do I
need to pass a reference to ClassA in ClassB's constructor (or whereever)?


Yes.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #3

P: n/a
"Jeff Johnson [MVP: VB]" <i.***@enough.spam> wrote in
news:e$**************@TK2MSFTNGP09.phx.gbl:

Yes, the nested class has access to all the members of the enclosing
class (or so the docs say) but I didn't see an example of how to
access the variables. I'll look it up when I get home.

As a guess, I'm sure you can just refer to the members by name. I'm
particularly interested in situations where you define other members
with the same names in the nested class and then you want to refer to
the member in the enclosing class. Perhaps the best advice in that
situation is "don't set it up that way," but let's see....


I figured it would but that doesn't seem to be the case. Maybe there's
some sort of default identifier (similar to MyBase or something) that needs
to be used but I can't find anything.

Maybe this is just bad design but in my particular situation it seems like
the best option.

Nov 21 '05 #4

P: n/a
* Joel Moore <as*******@asdaadad.com> scripsit:
Public ClassA
Friend varA As Integer
Private varB As Integer

Private ClassB
Public Sub MethodA()
' How can I access varA and varB here?
End Sub
End Class

End Class

is there anyway for the nested class "ClassB" to access the private or
friend variables contained in "ClassA"? Some sort of identifier? Or do I
need to pass a reference to ClassA in ClassB's constructor (or whereever)?


You cannot. Assume 'ClassB' is public. There is no guarantee that an
instance of the encosing class exists.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #5

P: n/a
Mattias Sjögren <ma********************@mvps.org> wrote in
news:eB**************@TK2MSFTNGP12.phx.gbl:
Joel,
Or do I
need to pass a reference to ClassA in ClassB's constructor (or
whereever)?


Yes.

Mattias


Yep, after a little more searching around I found some discussions about
this in some C# groups (inner and outer classes are the key words).
Apparently Java implicitly gives the inner class a reference to the outer
class which why this question usually pops up under the C# topics (since
many C# programmers are coming from a Java background).
Nov 21 '05 #6

P: n/a
hi***************@gmx.at (Herfried K. Wagner [MVP]) wrote in
news:u7**************@TK2MSFTNGP11.phx.gbl:

You cannot. Assume 'ClassB' is public. There is no guarantee that an
instance of the encosing class exists.


That makes sense. I never thought of it that way.

Nov 21 '05 #7

P: n/a

"Joel Moore" <as*******@asdaadad.com> wrote in message
news:Xn******************************@207.46.248.1 6...
Yes, the nested class has access to all the members of the enclosing
class (or so the docs say) but I didn't see an example of how to
access the variables. I'll look it up when I get home.

As a guess, I'm sure you can just refer to the members by name. I'm
particularly interested in situations where you define other members
with the same names in the nested class and then you want to refer to
the member in the enclosing class. Perhaps the best advice in that
situation is "don't set it up that way," but let's see....
I figured it would but that doesn't seem to be the case. Maybe there's
some sort of default identifier (similar to MyBase or something) that

needs to be used but I can't find anything.

Maybe this is just bad design but in my particular situation it seems like
the best option.


Well, the official word (as you've already discovered) is that you must
provide a reference to the enclosing class in order for the nested class to
access its members. However, doing so allows you to access Private members
of the enclosing class.

I based my original comment on the following passage from MSDN which was
talking about times that implementing a nested class is considered
acceptable:

----
If members of your class need to access private member variables of the
object containing it, you can implement it as a nested class. For example, a
Wheel object might have a private Radius field. If a Spoke object needs to
access the Radius field, it can always have that access if it is implemented
as a nested class.
----

I personally would have added "and you provide the nested class with a
reference to the enclosing class." Oh well.
Nov 21 '05 #8

P: n/a
Joel,
In addition to Herfried's comment about the outer (enclosing) class. I find
most of the time the inner class does not need a reference to the outer
class.

For example:

Public Class LinkedList

Private Class Node
Public Next As Node
Public Previous As Node
Public Data As Object
End Class

Private m_head As Node
Private m_tail As Node

End Class

Each node of the linked list above does not need a reference to the
LinkedList class itself, having such a reference causes more work for the GC
(it would need to track the references when collecting garbage).

Hope this helps
Jay
"Joel Moore" <as*******@asdaadad.com> wrote in message
news:Xn******************************@207.46.248.1 6...
hi***************@gmx.at (Herfried K. Wagner [MVP]) wrote in
news:u7**************@TK2MSFTNGP11.phx.gbl:

You cannot. Assume 'ClassB' is public. There is no guarantee that an
instance of the encosing class exists.


That makes sense. I never thought of it that way.

Nov 21 '05 #9

P: n/a
I agree, nested classes normally service the outer class and are nested for
convenience and privacy as they are normally only usefull outide it.

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
If U Need My Email ,Ask Me

Time flies when you don't know what you're doing

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:u0*************@TK2MSFTNGP09.phx.gbl...
Joel,
In addition to Herfried's comment about the outer (enclosing) class. I find most of the time the inner class does not need a reference to the outer
class.

For example:

Public Class LinkedList

Private Class Node
Public Next As Node
Public Previous As Node
Public Data As Object
End Class

Private m_head As Node
Private m_tail As Node

End Class

Each node of the linked list above does not need a reference to the
LinkedList class itself, having such a reference causes more work for the GC (it would need to track the references when collecting garbage).

Hope this helps
Jay
"Joel Moore" <as*******@asdaadad.com> wrote in message
news:Xn******************************@207.46.248.1 6...
hi***************@gmx.at (Herfried K. Wagner [MVP]) wrote in
news:u7**************@TK2MSFTNGP11.phx.gbl:

You cannot. Assume 'ClassB' is public. There is no guarantee that an
instance of the encosing class exists.


That makes sense. I never thought of it that way.


Nov 21 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.