Nested class: accessing containing class's variables

J

Joel Moore

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
 
J

Jeff Johnson [MVP: VB]

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

Joel Moore

Jeff Johnson said:
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.
 
H

Herfried K. Wagner [MVP]

* Joel Moore said:
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.
 
J

Joel Moore

Joel,


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).
 
J

Joel Moore

(e-mail address removed) (Herfried K. Wagner [MVP]) wrote in

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

Jeff Johnson

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

Jay B. Harlow [MVP - Outlook]

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
 
O

One Handed Man \( OHM - Terry Burns \)

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
 

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