Re: Need help with calling one class from another

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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



Relevant Pages

  • Re: Confirm bad news
    ... nested class within the same enclosing class. ... class B {private int secret;} ... members have scope throughout the compilation ...
    (comp.lang.java.programmer)
  • Re: Need help with calling one class from another
    ... is an object within the other class (InventoryItem). ... The line you say doesn't work isn't accessing the private field, ... I admit, from the code you posted, it's not obvious to me why you have the nested class anyway. ... 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. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Can nested class members access private members of nesting class?
    ... we are saying about class *Container* with some private members. ... > Nested class Node has all of its members as public so that Container ...
    (comp.lang.cpp)
  • Re: Magazine article
    ... websites. ... intended for private circulation among members of the ... and supplying information privately to family members. ... family members aren't family historians, ...
    (soc.genealogy.britain)
  • Re: Closing parenthesis in functions definition followed by its call
    ... "properties of the constructor"? ... Java's notion of static members, ... This pattern of public, private, and privileged members is possible ... The pattern in question is the one for emulating private instance ...
    (comp.lang.javascript)