473,486 Members | 2,407 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Nested class: accessing containing class's variables

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
9 3665

"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
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
"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
* 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
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
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

"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
29591
by: Joshua Beall | last post by:
Hi All, I remember reading that both nested classes and namespaces would be available in PHP5. I know that namespaces got canceled (much sadness...), however, I *thought* that nested classes...
3
2326
by: Nils Grimsmo | last post by:
hi, i'm having some trouble nesting functions. consider the following: def h(): x = 1 def g(): print x # ok, x is taken from h g()
6
2549
by: Andy Baker | last post by:
Hi there, I'm learning Python at the moment and trying to grok the thinking behind it's scoping and nesting rules. I was googling for nested functions and found this Guido quote:...
7
2233
by: block111 | last post by:
Hello, code like this: int f1(int x){ int f2(int y){ return y*y; } if(x > 0) return f2(x);
12
4007
by: Mortos | last post by:
I need some quick advice. I just need to reference a containing class from the nested class. I'm familiar with Java but C# is proving tricky. public BigClass { public int ID_BigClass = -99; ...
8
16850
by: Robert W. | last post by:
I've almost completed building a Model-View-Controller but have run into a snag. When an event is fired on a form control I want to automatically updated the "connnected" property in the Model. ...
37
3973
by: JohnGoogle | last post by:
Hi, Newbie question... After a recent article in VSJ I had a go at implementing a Fraction class to aid my understanding of operator overloading. After a previous message someone suggested...
4
2285
by: Wolfgang Draxinger | last post by:
If you know languages like Python or D you know, that nested functions can be really handy. Though some compilers (looking at GCC) provide the extension of nested functions, I wonder, how one...
3
3888
by: puzzlecracker | last post by:
Would you quickly remind me the difference between, regular class, static class, and nested class? Thanks
0
7100
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6964
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7175
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7330
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5434
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3070
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.