Re: Need help with calling one class from another
- From: "d.s." <nodamnspamok@xxxxxxxxx>
- Date: Mon, 21 Jul 2008 02:27:14 -0700 (PDT)
On Jul 20, 11:49 pm, "Peter Duniho" <NpOeStPe...@xxxxxxxxxxxxxxxx>
wrote:
On Sun, 20 Jul 2008 21:22:38 -0700, d.s. <nodamnspa...@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 agree it should work, but it is bombin on me. It is the code, but
it's trimmed way down, getting rid of other variables and their
accessors.
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?
Ths was a homework assignment. It has already been submitted, so
you're not doing my work for me. But I need to understand what went
wrong because other assignments will build on this. The teacher seems
to be having a bit of a problem responding to requests for help
(online class).
So, the assignment was to do it as done here. The next assignment was
to inherit the InventoryInfo class, which worked with no problems.
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.
But it should be able to use the accessors to gain access to the
private variables. Perhaps my terminology is wrong. I'm trying to
use the accessor, and it's blowing up:
********* return invInfo.companyName;
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.
I am trying to use the accessor to gain access to the private field.
********* return invInfo.companyName;
but it doesn't like it.
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).
The line I put the asterisks on is the first line that bombs in my
compiler.
If the contained class is to access the elements of the container
class, this means I will need to change the contained class everytime
I want to include it into a different container class that has
different elements.
.
- Follow-Ups:
- Re: Need help with calling one class from another
- From: Peter Duniho
- Re: Need help with calling one class from another
- From: Göran Andersson
- Re: Need help with calling one class from another
- References:
- Need help with calling one class from another
- From: d.s.
- Re: Need help with calling one class from another
- From: Peter Duniho
- Need help with calling one class from another
- Prev by Date: Re: Memory allocation
- Next by Date: Re: refelection (property)
- 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
|