Re: Need help with calling one class from another
- From: "Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx>
- Date: Sun, 20 Jul 2008 23:49:54 -0700
On Sun, 20 Jul 2008 21:22:38 -0700, d.s. <nodamnspamok@xxxxxxxxx> wrote:
I've got an app with two classes, and one class (InventoryInfoClass)
is an object within the other class (InventoryItem). I'm running into
problems with trying to access (get/set) a private variable within the
included class (InventoryInfo) from the "including" class
(InventoryItem).
First, the code you posted should work. The line you say doesn't work isn't accessing the private field, but rather a public property. Can we assume that the code you posted isn't in fact the code that you're having trouble with?
I admit, from the code you posted, it's not obvious to me why you have the nested class anyway. The outer, containing class is just proxying the value to the nested class anyway. Why not just put that data in the containing class?
That said, the issue you're seeing is a basic rule about nested classes: the nested class can see "private" members of the containing class, but not the other way around. That is, since the nested class is effectively a member the containing class, it has access to everything _any_ member of the containing class would, including all private members of the containing class. But the containing class is most certainly not a member of the nested class, and only the members of the nested class can access private members.
Personally, I don't see the problem here. I don't know why you want the nested class in the first place, but assuming you do, why not just use the public property as you've defined it, rather than trying to get at the private field? That's a better encapsulation anyway.
If you can elaborate on what code is actually not working, and why it is you think your containing class should have access to private members of the nested class, perhaps we can offer different advice more suitable to your goals (assuming the above doesn't do it).
Pete
.
- Follow-Ups:
- References:
- Prev by Date: Re: Need help with calling one class from another
- Next by Date: Re: IPAddress of connected client
- Previous by thread: Re: Need help with calling one class from another
- Next by thread: Re: Need help with calling one class from another
- Index(es):
Relevant Pages
|