Re: Private member variables

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Emil Kvarnhammar (kvarnhammarREMOVECAPS_at_home.se)
Date: 07/29/04


Date: Thu, 29 Jul 2004 15:00:41 +0200

I think Christoph means: Why is it that the private member is accessible
also on a local instance
of the class created within a member method, and not only for the current
object?

Example (current object):
privateVar = 99
// or
this.privateVar = 99

Example (Christoph's local variable in the Test() method)
myClass.privateVar = 99

Answer: What the private modifier actually does, is that it restrict access
to only the containing type.
This means that you can access the private member, as long as you access it
from anywhere within
the current type (MyClass methods etc.). Regardless which instance of the
type your'e accessing.
This is the correct design of an object-oriented language. (Same in the Java
language etc.).

/Emil Kvarnhammar

"benben" <benhongh@yahoo.com.au> wrote in message
news:OTVqbgWdEHA.2696@TK2MSFTNGP09.phx.gbl...
> Christoph,
>
> Private members of a class are meant to be accessed by other members of
the
> class. if privateVar is inaccessible by methods with its class, virtually
> nothing can ever touch it. Now this doesn't make sense since anything in a
> program must be used somewhere.
>
> When we decide the accessibility of a member within a class we are looking
> at a class-scope. That is, if a member is declared private, it is private
to
> THE CLASS so that it makes pretty good logic that the other members within
> the same class should have a right to access the (private) member. Your
> constructor MyClass.MyClass() actually implicitly initiates the privateVar
> member without you writing a line of code.
>
> A well designed class must maintain a proper degree of encapsulation. If
> something is not meant to be used by its client then by all means make it
> private. If somehow something is not meant to be used by the client but
> making it private will cause the class exceedingly difficult to reuse,
make
> it protected.
>
> ben
>
> > Let's take the following class:
> >
> > class MyClass {
> > private int privateVar;
> > public int PublicVar {
> > get { return privateVar; }
> > }
> >
> > public MyClass() {}
> >
> > public MyClass Test() {
> > MyClass myClass = new MyClass();
> > myClass.privateVar = 99;
> > return myClass;
> > }
> > }
> >
> > How is it that in the Test() method, my newly instantiated
> > 'myClass' variable can access the private 'privateVar' member?
> > Because the code is executing within a method of the 'MyClass'
> > class? Shouldn't it be that an object cannot access private
> > members regardless of where it's executed? Now, that is
> > true if I try to execute that exact same code in a method of
> > another class which is as it should be. I'm just curious what
> > is it that's telling both the compiler and the CLR that accessing
> > the private member in the above method is alright.
> >
> > thnx,
> > Christoph
> >
> >
>
>



Relevant Pages

  • Re: How do I write to a textbox in a form from within a class
    ... you can control the level of access for each class member. ... Protected Friend ... Private PrivateVar As Integer ... to your TextBox issue ... ...
    (microsoft.public.dotnet.languages.vb)
  • Re: "this" and "base"
    ... compiler and the programming language. ... a private inherited field in a base class from a derived class is that the ... > because the member "is a private". ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: "this" and "base"
    ... "What keeps you from accessing a Private member" is more of what you are ... It is both the Compiler and Runtime engine that keep you from ... member function in your derived class you could have a local variable called ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Why does python not have a mechanism for data hiding?
    ... define a new class member from completely outside the class ... that cannot be declared private. ... would need to use a "mangled" name to access private data or methods. ... without source code? ...
    (comp.lang.python)
  • Re: Ok Topper if you want to talk about "Troll Post" Addition
    ... Go ahead and waste time trying to find one instance where I posted PRIVATE, personal information about you... ... Knowingly spreading false rumors about another community member. ... Look at all the people that have asked you to stop posting to them, or sending email to them, and yet you persist. ...
    (misc.transport.trucking)