473,786 Members | 2,410 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

OOP question about properties & methods

Lets consider a class called Currency. This class must be the responsible for
taking care of all calculations over currency exchanges, in such a way that I
pass values in Euros and it returns the converted value in Dollar for
example...
Well, I might do this in 2 ways, as below:

1) I create a method like this:

double ConvertValue (double OriginalValue) {
// here I compute the value
return ComputedValue;
}
2) I create the method like this:

bool ConvertValue (double OriginalValue) {
// here I load private attribute ConvertedValue

ConvertedValue = ComputedValue

return OK;
}
In the first case I returned value in the method itself, in second case I
loaded the value in a private attribute. In second case the client method
would have to execute ConvertValue and then ask for property value. In first
case, the returned value would come in the method return itself....

What is the best approach? Are there special situations in which we should
use one or another??
Aug 12 '06
17 1530
Well Cor,

Thank you for the information on currency class/methods, I will take a look
at it...
But the example I put there was only to expose my concern on how I should
proceed when using properties (more elegant) vs returning values directly in
method return.
AFAIK, one big difference is that properties enable us to
use n times the value in its instance, without having to call the method
again (something like keeping its state).
Besides, properties might be welcome when we return more than
1 value in the same method. In this case, instead of using ref values, we
might load n properties and then retrieve them right afterwards...

Some fellows in this post have helped me posting that this True/False return
is not good, it should be replaced by exception control. That gives me even
less argument to choose the property way in cases I return just 1 value...

However I cant figure out those strict situations where I should choose one
or another...
"Cor Ligthert [MVP]" wrote:
Bruce,

I definitly would not make a class name Currency, the currencymanager it is
an important class that already exist to get the current rows in the
system.data namespace. I have seen that this currencymanager gives often
misunderstandin gs, but I would not make that more worse.

http://msdn.microsoft.com/library/de...classtopic.asp

Therefore I would name it at least LocalizedCurren cy or something.

Than it is of couse easy to return in that class the needed localized value
based on the localized settings of the computer.

The currencysymbol and other things are already in this and related member.

http://msdn2.microsoft.com/en-us/lib...ncysymbol.aspx

I hope this helps,

Cor

"Bruce One" <ra**@virtualso ftware.com.brsc hreef in bericht
news:E0******** *************** ***********@mic rosoft.com...
Lets consider a class called Currency. This class must be the responsible
for
taking care of all calculations over currency exchanges, in such a way
that I
pass values in Euros and it returns the converted value in Dollar for
example...
Well, I might do this in 2 ways, as below:

1) I create a method like this:

double ConvertValue (double OriginalValue) {
// here I compute the value
return ComputedValue;
}
2) I create the method like this:

bool ConvertValue (double OriginalValue) {
// here I load private attribute ConvertedValue

ConvertedValue = ComputedValue

return OK;
}
In the first case I returned value in the method itself, in second case I
loaded the value in a private attribute. In second case the client method
would have to execute ConvertValue and then ask for property value. In
first
case, the returned value would come in the method return itself....

What is the best approach? Are there special situations in which we should
use one or another??


Aug 13 '06 #11
Bruce One <ra**@virtualso ftware.com.brwr ote:
But the example I put there was only to expose my concern on how I should
proceed when using properties (more elegant) vs returning values directly in
method return.
You should use properties when they are:

1) Logically attributes of the class, i.e. similar to fields
2) Cheap to calculate and return
3) If reading doesn't modify the observable state of the class
4) If writeable, then writing and subsequently reading gives the same
value (i.e. { a.Prop = 42; Debug.Assert(a. Prop == 42); }), unless
writing the property throws an exception because of a problem with the
value.
5) No arguments are required to calculate the return value

When using properties that take arguments (i.e. 'public int this[int
index] { get; set; }'), you should restrict them to things like lists
and dictionaries, things that are usually collections and indexed by key
or index.

-- Barry

--
http://barrkel.blogspot.com/
Aug 13 '06 #12
Kelly,

Could your explain better points 3 and 4?

"Barry Kelly" wrote:
Bruce One <ra**@virtualso ftware.com.brwr ote:
But the example I put there was only to expose my concern on how I should
proceed when using properties (more elegant) vs returning values directly in
method return.

You should use properties when they are:

1) Logically attributes of the class, i.e. similar to fields
2) Cheap to calculate and return
3) If reading doesn't modify the observable state of the class
4) If writeable, then writing and subsequently reading gives the same
value (i.e. { a.Prop = 42; Debug.Assert(a. Prop == 42); }), unless
writing the property throws an exception because of a problem with the
value.
5) No arguments are required to calculate the return value

When using properties that take arguments (i.e. 'public int this[int
index] { get; set; }'), you should restrict them to things like lists
and dictionaries, things that are usually collections and indexed by key
or index.

-- Barry

--
http://barrkel.blogspot.com/
Aug 14 '06 #13
Bruce,

I would forever choose for a property if possible (in connection to my
previous reply) all was it alone that those are bindable, showable in a
property grid and serializable. Beside that do *I* find the text showed in
intellisence mostly standard better to understand from those.

When you want a direct answer, I never would take your method 2. It is only
confusing especially if the method is longer.

Cor

"Bruce One" <ra**@virtualso ftware.com.brsc hreef in bericht
news:82******** *************** ***********@mic rosoft.com...
Well Cor,

Thank you for the information on currency class/methods, I will take a
look
at it...
But the example I put there was only to expose my concern on how I should
proceed when using properties (more elegant) vs returning values directly
in
method return.
AFAIK, one big difference is that properties enable us to
use n times the value in its instance, without having to call the method
again (something like keeping its state).
Besides, properties might be welcome when we return more than
1 value in the same method. In this case, instead of using ref values, we
might load n properties and then retrieve them right afterwards...

Some fellows in this post have helped me posting that this True/False
return
is not good, it should be replaced by exception control. That gives me
even
less argument to choose the property way in cases I return just 1 value...

However I cant figure out those strict situations where I should choose
one
or another...
"Cor Ligthert [MVP]" wrote:
>Bruce,

I definitly would not make a class name Currency, the currencymanager it
is
an important class that already exist to get the current rows in the
system.data namespace. I have seen that this currencymanager gives often
misunderstandi ngs, but I would not make that more worse.

http://msdn.microsoft.com/library/de...classtopic.asp

Therefore I would name it at least LocalizedCurren cy or something.

Than it is of couse easy to return in that class the needed localized
value
based on the localized settings of the computer.

The currencysymbol and other things are already in this and related
member.

http://msdn2.microsoft.com/en-us/lib...ncysymbol.aspx

I hope this helps,

Cor

"Bruce One" <ra**@virtualso ftware.com.brsc hreef in bericht
news:E0******* *************** ************@mi crosoft.com...
Lets consider a class called Currency. This class must be the
responsible
for
taking care of all calculations over currency exchanges, in such a way
that I
pass values in Euros and it returns the converted value in Dollar for
example...
Well, I might do this in 2 ways, as below:

1) I create a method like this:

double ConvertValue (double OriginalValue) {
// here I compute the value
return ComputedValue;
}
2) I create the method like this:

bool ConvertValue (double OriginalValue) {
// here I load private attribute ConvertedValue

ConvertedValue = ComputedValue

return OK;
}
In the first case I returned value in the method itself, in second case
I
loaded the value in a private attribute. In second case the client
method
would have to execute ConvertValue and then ask for property value. In
first
case, the returned value would come in the method return itself....

What is the best approach? Are there special situations in which we
should
use one or another??




Aug 14 '06 #14
Cor,
So if you dont like executing a void method and then calling for the
property return, what would the best approach?
My great fear about using properties, instead of returning in method return
itself is that client code is unaware of what properties exaclty he should
reference to get the values he wants...

"Cor Ligthert [MVP]" wrote:
Bruce,

I would forever choose for a property if possible (in connection to my
previous reply) all was it alone that those are bindable, showable in a
property grid and serializable. Beside that do *I* find the text showed in
intellisence mostly standard better to understand from those.

When you want a direct answer, I never would take your method 2. It is only
confusing especially if the method is longer.

Cor

"Bruce One" <ra**@virtualso ftware.com.brsc hreef in bericht
news:82******** *************** ***********@mic rosoft.com...
Well Cor,

Thank you for the information on currency class/methods, I will take a
look
at it...
But the example I put there was only to expose my concern on how I should
proceed when using properties (more elegant) vs returning values directly
in
method return.
AFAIK, one big difference is that properties enable us to
use n times the value in its instance, without having to call the method
again (something like keeping its state).
Besides, properties might be welcome when we return more than
1 value in the same method. In this case, instead of using ref values, we
might load n properties and then retrieve them right afterwards...

Some fellows in this post have helped me posting that this True/False
return
is not good, it should be replaced by exception control. That gives me
even
less argument to choose the property way in cases I return just 1 value...

However I cant figure out those strict situations where I should choose
one
or another...
"Cor Ligthert [MVP]" wrote:
Bruce,

I definitly would not make a class name Currency, the currencymanager it
is
an important class that already exist to get the current rows in the
system.data namespace. I have seen that this currencymanager gives often
misunderstandin gs, but I would not make that more worse.

http://msdn.microsoft.com/library/de...classtopic.asp

Therefore I would name it at least LocalizedCurren cy or something.

Than it is of couse easy to return in that class the needed localized
value
based on the localized settings of the computer.

The currencysymbol and other things are already in this and related
member.

http://msdn2.microsoft.com/en-us/lib...ncysymbol.aspx

I hope this helps,

Cor

"Bruce One" <ra**@virtualso ftware.com.brsc hreef in bericht
news:E0******** *************** ***********@mic rosoft.com...
Lets consider a class called Currency. This class must be the
responsible
for
taking care of all calculations over currency exchanges, in such a way
that I
pass values in Euros and it returns the converted value in Dollar for
example...
Well, I might do this in 2 ways, as below:

1) I create a method like this:

double ConvertValue (double OriginalValue) {
// here I compute the value
return ComputedValue;
}
2) I create the method like this:

bool ConvertValue (double OriginalValue) {
// here I load private attribute ConvertedValue

ConvertedValue = ComputedValue

return OK;
}
In the first case I returned value in the method itself, in second case
I
loaded the value in a private attribute. In second case the client
method
would have to execute ConvertValue and then ask for property value. In
first
case, the returned value would come in the method return itself....

What is the best approach? Are there special situations in which we
should
use one or another??




Aug 14 '06 #15
Bruce,

Using intelisense is the simplest method to tell the client all he wants to
know, it can be showed in the propertygrid and while selecting it in
intelisence by the comments.

http://msdn2.microsoft.com/en-us/library/z04awywx.aspx

Cor

"Bruce One" <ra**@virtualso ftware.com.brsc hreef in bericht
news:A4******** *************** ***********@mic rosoft.com...
Cor,
So if you dont like executing a void method and then calling for the
property return, what would the best approach?
My great fear about using properties, instead of returning in method
return
itself is that client code is unaware of what properties exaclty he should
reference to get the values he wants...

"Cor Ligthert [MVP]" wrote:
>Bruce,

I would forever choose for a property if possible (in connection to my
previous reply) all was it alone that those are bindable, showable in a
property grid and serializable. Beside that do *I* find the text showed
in
intellisence mostly standard better to understand from those.

When you want a direct answer, I never would take your method 2. It is
only
confusing especially if the method is longer.

Cor

"Bruce One" <ra**@virtualso ftware.com.brsc hreef in bericht
news:82******* *************** ************@mi crosoft.com...
Well Cor,

Thank you for the information on currency class/methods, I will take a
look
at it...
But the example I put there was only to expose my concern on how I
should
proceed when using properties (more elegant) vs returning values
directly
in
method return.
AFAIK, one big difference is that properties enable us to
use n times the value in its instance, without having to call the
method
again (something like keeping its state).
Besides, properties might be welcome when we return more than
1 value in the same method. In this case, instead of using ref values,
we
might load n properties and then retrieve them right afterwards...

Some fellows in this post have helped me posting that this True/False
return
is not good, it should be replaced by exception control. That gives me
even
less argument to choose the property way in cases I return just 1
value...

However I cant figure out those strict situations where I should choose
one
or another...
"Cor Ligthert [MVP]" wrote:

Bruce,

I definitly would not make a class name Currency, the currencymanager
it
is
an important class that already exist to get the current rows in the
system.data namespace. I have seen that this currencymanager gives
often
misunderstandi ngs, but I would not make that more worse.

http://msdn.microsoft.com/library/de...classtopic.asp

Therefore I would name it at least LocalizedCurren cy or something.

Than it is of couse easy to return in that class the needed localized
value
based on the localized settings of the computer.

The currencysymbol and other things are already in this and related
member.

http://msdn2.microsoft.com/en-us/lib...ncysymbol.aspx

I hope this helps,

Cor

"Bruce One" <ra**@virtualso ftware.com.brsc hreef in bericht
news:E0******* *************** ************@mi crosoft.com...
Lets consider a class called Currency. This class must be the
responsible
for
taking care of all calculations over currency exchanges, in such a
way
that I
pass values in Euros and it returns the converted value in Dollar
for
example...
Well, I might do this in 2 ways, as below:

1) I create a method like this:

double ConvertValue (double OriginalValue) {
// here I compute the value
return ComputedValue;
}
2) I create the method like this:

bool ConvertValue (double OriginalValue) {
// here I load private attribute ConvertedValue

ConvertedValue = ComputedValue

return OK;
}
In the first case I returned value in the method itself, in second
case
I
loaded the value in a private attribute. In second case the client
method
would have to execute ConvertValue and then ask for property value.
In
first
case, the returned value would come in the method return itself....

What is the best approach? Are there special situations in which we
should
use one or another??





Aug 14 '06 #16
Bruce One <ra**@virtualso ftware.com.brwr ote:
Kelly,
My name is Barry!
Could your explain better points 3 and 4?
3) If reading doesn't modify the observable state of the class
Just reading a property shouldn't change the property. For example:

Button b = new Button();
// ...
string text = b.Text;

This action (reading b.Text, which is a property) shouldn't change the
property. Another example:

IEnumerator x = new List().GetEnume rator();
bool b = x.MoveNext();

This action, MoveNext(), changes the enumerator - it moves it forward
one. Therefore it shouldn't be a property, it should be a method (which
it is).
4) If writeable, then writing and subsequently reading gives the same
value (i.e. { a.Prop = 42; Debug.Assert(a. Prop == 42); }), unless
writing the property throws an exception because of a problem with the
value.
When there is a property, people expect this:

b.Name = "foo";
if (b.Name != "foo")
throw new Exception("Some thing went wrong!");

If this code would throw an exception for your property, then it
shouldn't be a property - it should be a method.

This only matters if the property is writeable - that is, it has a 'set'
method.

-- Barry

--
http://barrkel.blogspot.com/
Aug 14 '06 #17
Barry, sorry for misusing your name.
I think I quite understood your points, I will try to fit our cases in one
of these situations.

Thanks!

Aug 14 '06 #18

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

Similar topics

2
1171
by: baylor | last post by:
Just to verify, i cannot use properties and have a public get and a protected set, correct? i assumed i could either define it twice with different scopes or set the access visibility at the get/set level but neither is true. So i assume this means i have to stop using properties and use the Java-style getX()/setX() methods. But i wanted to verify before i start doing that -baylor
5
1750
by: s_m_b | last post by:
function saveState() { document.write (" | <a href = 'myhomepage.asp? view=mhp&amp;action=save&amp;pb="); >> document.write (document.phone.style.display.value); document.write ("'>save view</a>"); }; creates an error at this (>) line.
3
1611
by: Sam Sungshik Kong | last post by:
Hello! While using panel control, I wondered a thing. Panel class is subclass of Control class. Control class has KeyPress event and Focus() method, etc... Then Panel class must have them. I guess it *has* then behind even if they are not meaningful. However, the code complete tool doesn't show them when I type panel1.(code complete list). Actually I can type panel1.Focus() without any compilation error.
3
2575
by: MeNotHome | last post by:
I have been trying to find all the properties and methods documentation on the axwebbrowser control. For example a list of follwing and their results axwebbrowser.document.body.outerhtml axwebbrowser.document.body.innerhtml etc etc. thx
13
3035
by: usenet | last post by:
How and where can one find out about the basics of VB/Access2003 syntax? I am a died in the wool C/C++/Java Linux/Unix programmer and I am finding it difficult to understand the program format for accessing objects, controls, etc. in VB/Access2003. In particular where will I find explanations of:- Actions, Functions, Methods, Properties - I'm understand the
3
2169
by: redefined.horizons | last post by:
I've been reading about Python Classes, and I'm a little confused about how Python stores the state of an object. I was hoping for some help. I realize that you can't create an empty place holder for a member variable of a Python object. It has to be given a value when defined, or set within a method. But what is the difference between an Attribute of a Class, a Descriptor in a Class, and a Property in a Class?
3
2318
by: jibesh.vp | last post by:
Hi all, I have a doubt in using C# Reflection mechanism . What i need is to get method info alone from a class type. Following code gets all the Properties inside the class too and there is no way to differentiate a method from property.
18
1932
by: Tom Cole | last post by:
I'm working on a small Ajax request library to simplify some tasks that I will be taking on shortly. For the most part everything works fine, however I seem to have some issues when running two requests at the same time. The first one stops execution as the second continues. If I place either an alert between the two requests or run the second through a setTimeout of only 1 millisecond, they both work. You can see a working example here:...
25
2093
by: raylopez99 | last post by:
First in an occasional series. I'm somewhat experienced in C++, and am using .NET Visual Studio 2005 as the IDE. I'm learning C#. What I don't like about C#, compared to C++ (all flavors): 1/ no pointers or tracking pointers or handles--this is absurd. I realise references are by and large like tracking pointers effectively (does anybody know of any differences--other than pointer arithmetic,
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10360
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10108
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9960
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7510
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4064
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.