473,387 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Basic OO question...

I am trying to determine how far I should go in encapsulating data. As I
understand it, OO practices state to create private member variables and use
these variables in your publicly accessible functions. This requires the
programmer to set the variables using the setters of the class before
calling the function. The problem that I see is that the coder doesn't know
the private variables needed by the public function without viewing the
actual code (since there is no signature, intellisense tells them nothing).
Furthermore, overloading becomes troublesome. Maybe I am over-engineering
the use of the private members in a class in regards to methods. Should
methods always have a signature and when should we use private members. I
am probably confusing myself :)
Nov 19 '05 #1
7 1337
Not sure to understand. Why would the coder need to know what are the
private members ? There is no need for him to know...
Also it looks like there is some confusion between methods, singatures and
the like.

It looks like the principle you are referring two is to expose "values" as
"properties" rather than "public fields".

The reason for this is that the former allows to run addtional check (as
your code runs) while you would have no control on the later. Even if you
does nothing for now, it would likely break later (as previously compiled
code would use an assignment where it would be know a call to the "setter").

For these reasons it is considered best practice to access to "values"
through properties (set/get) rather than exposing directly the value as a
"field".

(I'm not even sure to use the correct words myself ;-)

Patrice

--

"Bob Morvay" <r@r.com> a écrit dans le message de
news:O8*************@TK2MSFTNGP09.phx.gbl...
I am trying to determine how far I should go in encapsulating data. As I
understand it, OO practices state to create private member variables and use these variables in your publicly accessible functions. This requires the
programmer to set the variables using the setters of the class before
calling the function. The problem that I see is that the coder doesn't know the private variables needed by the public function without viewing the
actual code (since there is no signature, intellisense tells them nothing). Furthermore, overloading becomes troublesome. Maybe I am over-engineering
the use of the private members in a class in regards to methods. Should
methods always have a signature and when should we use private members. I
am probably confusing myself :)

Nov 19 '05 #2
You use private member fields for data which describes the object. You use
these private fields within your public functions when the function alters
the behaviour/data of your object. If you aren't clear whether to use
private fields w/public properties vs function parameters, something's
wrong. Typically they two aren't interchangable so I'm not sure why you are
confused. Private members exist for the duration of the object (ie as long
as you hold a reference to it). Parameters that you pass into a function
are lost at the end of the function call (unless you store there somewhere
yourself).

let's try another way, function parameters are typically used by said
function and only said function...they are scopped to the function and no
other parts of the class (they can be used by other parts of the calling
code of course). Private members are scopped to the entire class and hold
data for use by the entire class.

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"Bob Morvay" <r@r.com> wrote in message
news:O8*************@TK2MSFTNGP09.phx.gbl...
I am trying to determine how far I should go in encapsulating data. As I
understand it, OO practices state to create private member variables and
use these variables in your publicly accessible functions. This requires
the programmer to set the variables using the setters of the class before
calling the function. The problem that I see is that the coder doesn't
know the private variables needed by the public function without viewing
the actual code (since there is no signature, intellisense tells them
nothing). Furthermore, overloading becomes troublesome. Maybe I am
over-engineering the use of the private members in a class in regards to
methods. Should methods always have a signature and when should we use
private members. I am probably confusing myself :)

Nov 19 '05 #3
Hi Bob,

To access private variables you can use public/protected properties you need
not to use functions in C# and VB.NET. J# doesnot support properties
directly and C++.NET properties are implemented with using functions. My
advice you take a look at VS.NET 2005 for promoting functions and
properties. I think it does what u need, if i fully understand what u mean.

--

Thanks,
Yunus Emre ALPÖZEN
BSc, MCAD.NET

"Bob Morvay" <r@r.com> wrote in message
news:O8*************@TK2MSFTNGP09.phx.gbl...
I am trying to determine how far I should go in encapsulating data. As I
understand it, OO practices state to create private member variables and
use these variables in your publicly accessible functions. This requires
the programmer to set the variables using the setters of the class before
calling the function. The problem that I see is that the coder doesn't
know the private variables needed by the public function without viewing
the actual code (since there is no signature, intellisense tells them
nothing). Furthermore, overloading becomes troublesome. Maybe I am
over-engineering the use of the private members in a class in regards to
methods. Should methods always have a signature and when should we use
private members. I am probably confusing myself :)

Nov 19 '05 #4
Hi Bob,

Good questions. I'm encouraged to see someone really trying to understand
this stuff instead of just using it!
As I understand it, OO practices state to create private member variables
and use these variables in your publicly accessible functions. This
requires the programmer to set the variables using the setters of the
class before calling the function.
It helps if you differentiate the term "Properties" from the term
"Functions." In a sense, a Property can actually BE 2 functions, one for
setting, and one for getting. It can actually be only one of these, hence
"read only" and "write only." A Property is actually a sort of "unholy
marriage" of a variable and one or 2 methods ("functions"). And, in fact, a
Property doesn't even have to relate directly to a private variable (or
"field" which is the term for a variable that is global to the class) at
all. Since a Property is actually one or two methods, these methods can do
virtually anything the designer wants them to. The only requirement is that
the getter must return a value of the type of the Property. So, for example,
let's talk about a property called "area" of a geometric figure. It could be
a "read only" property that calculates the area of the geometric figure, and
returns the value. In fact, there might not be a private "area" variable at
all, but simply the method that calculates it and returns it.

Hopefully, you are beginning to see the power of Properties at this point.
This requires the programmer to set the variables using the setters of the
class before calling the function. The problem that I see is that the
coder doesn't know the private variables needed by the public function
without viewing the actual code (since there is no signature, intellisense
tells them nothing).
Now we need to talk a bit about another OOP characteristic called
"Abstraction." You have a car. You start it by turning the key. So, let's
imagine a software car. It has a "read only" property called "Started." You
set "Started to true ("turning the key," so to speak), and the engine starts
up. What in fact has happened? The Started Set method is a function that
performs all the steps necessary to start the car. So, how much does the
driver (programmer) need to know about how the engine works? Nothing.
Nothing at all. All the driver needs to know is how to start the car.

Encapsulation hides all of the mechanism of the car's engine "under the
hood" in a "black box." Only the designer of the car actually knows how the
engine works. All you need is to get to work in the car. So, you don't need
to know how much air and gas to pump into the carbeurator, when to send the
spark, etc. All you need to do is start it and drive.

Okay, so now you're designing a car. How much do you need to know about it?
Well, if you build it entirely from scratch, you need to know quite a bit.
On the other hand, if you buy ready-made parts (alternator, transmission,
engine block, etc.), you are employing other objects in the design of your
car. You don't need to know how each of them works, only how they interface,
or interact, with the other parts of the car, and what their operating
characteristics are. This is simply an analogy for programming with the CLR.
The CLR is like a bunch of auto parts. How does a Socket operate internally?
Who cares? You can get one from the CLR and configure it to work in your car
(class).

If you understand these principles, you should be able to design your
classes so that, in essence, they are foolproof. As long as the programmer
that uses them knows how they interoperate in an application, they know
enough. The class itself should handle the rest.

So, the developer doesn't NEED to know anything about private members.
Private members are for the use of your class, not for the use of the
developer using it. Design your classes well, using the principles of OOP,
and your drivers will thank you for it! :)

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.

"Bob Morvay" <r@r.com> wrote in message
news:O8*************@TK2MSFTNGP09.phx.gbl...I am trying to determine how far I should go in encapsulating data. As I
understand it, OO practices state to create private member variables and
use these variables in your publicly accessible functions. This requires
the programmer to set the variables using the setters of the class before
calling the function. The problem that I see is that the coder doesn't
know the private variables needed by the public function without viewing
the actual code (since there is no signature, intellisense tells them
nothing). Furthermore, overloading becomes troublesome. Maybe I am
over-engineering the use of the private members in a class in regards to
methods. Should methods always have a signature and when should we use
private members. I am probably confusing myself :)

Nov 19 '05 #5
Correction: When I spoke of the "Started" property of the car illustration,
I said "read only." I should have said "write only."

--

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.

"Bob Morvay" <r@r.com> wrote in message
news:O8*************@TK2MSFTNGP09.phx.gbl...
I am trying to determine how far I should go in encapsulating data. As I
understand it, OO practices state to create private member variables and
use these variables in your publicly accessible functions. This requires
the programmer to set the variables using the setters of the class before
calling the function. The problem that I see is that the coder doesn't
know the private variables needed by the public function without viewing
the actual code (since there is no signature, intellisense tells them
nothing). Furthermore, overloading becomes troublesome. Maybe I am
over-engineering the use of the private members in a class in regards to
methods. Should methods always have a signature and when should we use
private members. I am probably confusing myself :)

Nov 19 '05 #6
I think I get the point about abstraction but I have to ask the question how
does one know to use a key to start the car. In technical terms this may be
to execute the setter for a key that will be used to start the car. The car
method would not have any parameters but will expect that a key is ready for
use. Developers will need to know that they need a key to start the car.
How do you expose to the developer that he needs to use a key in order to
execute the start process of a car without exposing the start process?
Sheesh, I hope I haven't overdone the car example. This can be applied to
any method of a type of class. In regards to overloading, doesn't
signature-less methods really mess with overloading. Especially if the
method is using private member variables that get set outside of the method
with public properties?
"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
Hi Bob,

Good questions. I'm encouraged to see someone really trying to understand
this stuff instead of just using it!
As I understand it, OO practices state to create private member variables
and use these variables in your publicly accessible functions. This
requires the programmer to set the variables using the setters of the
class before calling the function.


It helps if you differentiate the term "Properties" from the term
"Functions." In a sense, a Property can actually BE 2 functions, one for
setting, and one for getting. It can actually be only one of these, hence
"read only" and "write only." A Property is actually a sort of "unholy
marriage" of a variable and one or 2 methods ("functions"). And, in fact,
a Property doesn't even have to relate directly to a private variable (or
"field" which is the term for a variable that is global to the class) at
all. Since a Property is actually one or two methods, these methods can do
virtually anything the designer wants them to. The only requirement is
that the getter must return a value of the type of the Property. So, for
example, let's talk about a property called "area" of a geometric figure.
It could be a "read only" property that calculates the area of the
geometric figure, and returns the value. In fact, there might not be a
private "area" variable at all, but simply the method that calculates it
and returns it.

Hopefully, you are beginning to see the power of Properties at this point.
This requires the programmer to set the variables using the setters of
the class before calling the function. The problem that I see is that
the coder doesn't know the private variables needed by the public
function without viewing the actual code (since there is no signature,
intellisense tells them nothing).


Now we need to talk a bit about another OOP characteristic called
"Abstraction." You have a car. You start it by turning the key. So, let's
imagine a software car. It has a "read only" property called "Started."
You set "Started to true ("turning the key," so to speak), and the engine
starts up. What in fact has happened? The Started Set method is a function
that performs all the steps necessary to start the car. So, how much does
the driver (programmer) need to know about how the engine works? Nothing.
Nothing at all. All the driver needs to know is how to start the car.

Encapsulation hides all of the mechanism of the car's engine "under the
hood" in a "black box." Only the designer of the car actually knows how
the engine works. All you need is to get to work in the car. So, you don't
need to know how much air and gas to pump into the carbeurator, when to
send the spark, etc. All you need to do is start it and drive.

Okay, so now you're designing a car. How much do you need to know about
it? Well, if you build it entirely from scratch, you need to know quite a
bit. On the other hand, if you buy ready-made parts (alternator,
transmission, engine block, etc.), you are employing other objects in the
design of your car. You don't need to know how each of them works, only
how they interface, or interact, with the other parts of the car, and what
their operating characteristics are. This is simply an analogy for
programming with the CLR. The CLR is like a bunch of auto parts. How does
a Socket operate internally? Who cares? You can get one from the CLR and
configure it to work in your car (class).

If you understand these principles, you should be able to design your
classes so that, in essence, they are foolproof. As long as the programmer
that uses them knows how they interoperate in an application, they know
enough. The class itself should handle the rest.

So, the developer doesn't NEED to know anything about private members.
Private members are for the use of your class, not for the use of the
developer using it. Design your classes well, using the principles of OOP,
and your drivers will thank you for it! :)

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
What You Seek Is What You Get.

"Bob Morvay" <r@r.com> wrote in message
news:O8*************@TK2MSFTNGP09.phx.gbl...
I am trying to determine how far I should go in encapsulating data. As I
understand it, OO practices state to create private member variables and
use these variables in your publicly accessible functions. This requires
the programmer to set the variables using the setters of the class before
calling the function. The problem that I see is that the coder doesn't
know the private variables needed by the public function without viewing
the actual code (since there is no signature, intellisense tells them
nothing). Furthermore, overloading becomes troublesome. Maybe I am
over-engineering the use of the private members in a class in regards to
methods. Should methods always have a signature and when should we use
private members. I am probably confusing myself :)


Nov 19 '05 #7
Hi Bob,

Okay, the car is the class, not a method. Abstraction means that you can
treat the class as if it were a "real world" kind of device. So, in my
analogy, the car is a class.

So, we have a class called "car" and a property (write only) called
"Started." All you need to do is write documentation that tells the
developer that to "start" the "car class" he sets the "Started" property to
true. Again, using my analogy, the "Started" property IS the key. It is what
starts and stops the car's engine. When you set the property to false (not
Started), the property's set method performs all the steps to shut the
engine down, such as cutting off the air flow and gas flow, stopping the
spark, etc. You could also make the Started property read-write, use a
private field called _Started that is set in the setter, and use the get
method to find out if the car is started by returning the value of _Started.
I was trying to keep it simple (for a "Start" ;-)).

Overloading is another topic altogether, and has to do more with Inheritance
than Encapsulation. I'd be happy to discuss it, but it is important to have
these concepts down pat first! I will say that it is important to design
classes well in order to make the best use of them. Encapsulation can be a
force for great good or evil. It is also important to document them well if
you expect people to use them effectively and/or extend them. Of course,
anyone can break just about any software they use if they are either clever
and/or stupid enough!

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.

"Bob Morvay" <r@r.com> wrote in message
news:%2******************@TK2MSFTNGP14.phx.gbl...
I think I get the point about abstraction but I have to ask the question
how does one know to use a key to start the car. In technical terms this
may be to execute the setter for a key that will be used to start the car.
The car method would not have any parameters but will expect that a key is
ready for use. Developers will need to know that they need a key to start
the car. How do you expose to the developer that he needs to use a key in
order to execute the start process of a car without exposing the start
process? Sheesh, I hope I haven't overdone the car example. This can be
applied to any method of a type of class. In regards to overloading,
doesn't signature-less methods really mess with overloading. Especially if
the method is using private member variables that get set outside of the
method with public properties?
"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
Hi Bob,

Good questions. I'm encouraged to see someone really trying to understand
this stuff instead of just using it!
As I understand it, OO practices state to create private member
variables and use these variables in your publicly accessible functions.
This requires the programmer to set the variables using the setters of
the class before calling the function.


It helps if you differentiate the term "Properties" from the term
"Functions." In a sense, a Property can actually BE 2 functions, one for
setting, and one for getting. It can actually be only one of these, hence
"read only" and "write only." A Property is actually a sort of "unholy
marriage" of a variable and one or 2 methods ("functions"). And, in fact,
a Property doesn't even have to relate directly to a private variable (or
"field" which is the term for a variable that is global to the class) at
all. Since a Property is actually one or two methods, these methods can
do virtually anything the designer wants them to. The only requirement is
that the getter must return a value of the type of the Property. So, for
example, let's talk about a property called "area" of a geometric figure.
It could be a "read only" property that calculates the area of the
geometric figure, and returns the value. In fact, there might not be a
private "area" variable at all, but simply the method that calculates it
and returns it.

Hopefully, you are beginning to see the power of Properties at this
point.
This requires the programmer to set the variables using the setters of
the class before calling the function. The problem that I see is that
the coder doesn't know the private variables needed by the public
function without viewing the actual code (since there is no signature,
intellisense tells them nothing).


Now we need to talk a bit about another OOP characteristic called
"Abstraction." You have a car. You start it by turning the key. So, let's
imagine a software car. It has a "read only" property called "Started."
You set "Started to true ("turning the key," so to speak), and the engine
starts up. What in fact has happened? The Started Set method is a
function that performs all the steps necessary to start the car. So, how
much does the driver (programmer) need to know about how the engine
works? Nothing. Nothing at all. All the driver needs to know is how to
start the car.

Encapsulation hides all of the mechanism of the car's engine "under the
hood" in a "black box." Only the designer of the car actually knows how
the engine works. All you need is to get to work in the car. So, you
don't need to know how much air and gas to pump into the carbeurator,
when to send the spark, etc. All you need to do is start it and drive.

Okay, so now you're designing a car. How much do you need to know about
it? Well, if you build it entirely from scratch, you need to know quite a
bit. On the other hand, if you buy ready-made parts (alternator,
transmission, engine block, etc.), you are employing other objects in the
design of your car. You don't need to know how each of them works, only
how they interface, or interact, with the other parts of the car, and
what their operating characteristics are. This is simply an analogy for
programming with the CLR. The CLR is like a bunch of auto parts. How does
a Socket operate internally? Who cares? You can get one from the CLR and
configure it to work in your car (class).

If you understand these principles, you should be able to design your
classes so that, in essence, they are foolproof. As long as the
programmer that uses them knows how they interoperate in an application,
they know enough. The class itself should handle the rest.

So, the developer doesn't NEED to know anything about private members.
Private members are for the use of your class, not for the use of the
developer using it. Design your classes well, using the principles of
OOP, and your drivers will thank you for it! :)

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
What You Seek Is What You Get.

"Bob Morvay" <r@r.com> wrote in message
news:O8*************@TK2MSFTNGP09.phx.gbl...
I am trying to determine how far I should go in encapsulating data. As I
understand it, OO practices state to create private member variables and
use these variables in your publicly accessible functions. This requires
the programmer to set the variables using the setters of the class before
calling the function. The problem that I see is that the coder doesn't
know the private variables needed by the public function without viewing
the actual code (since there is no signature, intellisense tells them
nothing). Furthermore, overloading becomes troublesome. Maybe I am
over-engineering the use of the private members in a class in regards to
methods. Should methods always have a signature and when should we use
private members. I am probably confusing myself :)



Nov 19 '05 #8

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

Similar topics

6
by: pauldepstein | last post by:
I am reading Grimshaw and Ortega's "C++ and Numerical Methods." They construct a vector class which contains the variable vec, a float* variable where the length of the array (number of...
6
by: DH | last post by:
I have a VERY basic question about figuring database size. I've inherited a database which is generally similar to this basic one: Item, Red, Blue, Green, Yellow (text), (int),(int),(int),(int)...
9
by: Malcolm | last post by:
After some days' hard work I am now the proud possessor of an ANSI C BASIC interpreter. The question is, how is it most useful? At the moment I have a function int basic(const char *script,...
4
by: Ramesh | last post by:
hi, Let me ask some basic questions. Can anybody explain me about the following questions: 1. When we have to create sn key? Whenever we compiled Component we have to create or it is a one time...
13
by: Pete | last post by:
I'm cross posting from mscom.webservices.general as I have received no answer there: There has been a number of recent posts requesting how to satisfactorily enable BASIC authorization at the...
5
by: Aussie Rules | last post by:
Hi, Having a mental block on this one. Have done it before but can't rack my brain on how... I have an object, with a bunch on property, and I add that object to a combo box. I want the...
4
by: MikeB | last post by:
I've been all over the net with this question, I hope I've finally found a group where I can ask about Visual Basic 2005. I'm at uni and we're working with Visual Basic 2005. I have some books, ...
1
by: frankhanretty | last post by:
Do I have to install Visual basic on the remote terminals as I did on the server? I have an visual basic 5 application running fine on my client's server and he is now networked. He wants to run the...
4
by: Chris Asaipillai | last post by:
Hi there My compay has a number of Visual Basic 6 applications which are front endeed onto either SQL Server or Microsoft Access databases. Now we are in process of planning to re-write these...
3
by: Scott Stark | last post by:
Hello, I'm trying to get a better handle on OOP programming principles in VB.NET. Forgive me if this question is sort of basic, but here's what I want to do. I have a collection of Employee...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.