472,794 Members | 2,754 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,794 software developers and data experts.

Private member variables

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
Nov 16 '05 #1
2 1776
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

Nov 16 '05 #2
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" <be******@yahoo.com.au> wrote in message
news:OT**************@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


Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: IHateSuperman | last post by:
public class StaticField2{ public static void main(String args){ private int x, y; // <<== error 1 for ( y = 0 ; y < 100 ; y++){ x = StaticMethod(); System.out.println(" x = "+x); } } public...
10
by: Scott Brady Drummonds | last post by:
Hi, everyone, I'm still learning Python as I develop a medium-sized project. From my previous experience with C++, I've burnt into my mind the notion of information hiding. I'm having trouble...
3
by: Rajesh Garg | last post by:
Can we have private constructors and destructors? IF yes what is the use of such constructors or destructors.....in the sense where can these be implemented in a system................. I have...
19
by: qazmlp | last post by:
class base { // other members public: virtual ~base() { } virtual void virtualMethod1()=0 ; virtual void virtualMethod2()=0 ; virtual void virtualMethod3()=0 ;
6
by: David Whitchurch-Bennett | last post by:
Hi There, I have created a very simple web user control, containing only a combo box. I have created a class which contains some private object variables, for example... Private _anObject as...
23
by: Ben Voigt | last post by:
I have a POD type with a private destructor. There are a whole hierarchy of derived POD types, all meant to be freed using a public member function Destroy in the base class. I get warning C4624....
6
by: zfareed | last post by:
Is it possible to print an array that is declared from a class type that also contains private data members? I have declared an array in the main function and then set the values for the array...
8
by: David Veeneman | last post by:
Should a member variable be passed to a private method in the same class as a method argument, or should the method simply call the member variable? For years, I have passed member variables to...
5
by: fourpastmidnight | last post by:
Ok, before anyone gets on me ( ;) ), I'm developing on Windows 2000 with IE6sp1 (that's what my company uses). So no, I can't use Firefox, though I wish I could. Ok, with that out of the way, here's...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.