Re: Need help with calling one class from another

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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
.



Relevant Pages

  • 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: 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: inner class scope issues
    ... It is contradictory to name a static nested class with the word ... The correct mindset seems to be to think about inheritance of private ... access a private member" when on every other line you are! ...
    (comp.lang.java.programmer)
  • Re: Need help with calling one class from another
    ... isn't accessing the private field, ... value to the nested class anyway. ... the nested class can see "private" members of the containing class, ... private variables. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: error CS0621: virtual or abstract members cannot be private
    ... private virtual Doit ... It seems like a very strange pattern to me to think that a) _any_ code would be able to directly execute Derived's Doitimplementation, and also that b) the only code that theoretically could do so would be in the very class where Derived is declared (i.e. you will _never_ be in a situation where the Derived type's method is used by code that has knowledge only of the Base class, the usual reason we use polymorphism in the first place). ... Personally, to me it seems that even having a nested class inherit the containing class is a step in the wrong direction to start with, never mind the odd polymorphic construct. ...
    (microsoft.public.dotnet.languages.csharp)