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?? 17 1529
Bruce One <ra**@virtualso ftware.com.brwr ote:
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:
<snip>
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??
The first approach is definitely better. It means:
1) The caller only has to do one thing in order to do what they want -
get the converted value.
2) If you use exceptions to indicate unexpected failures, the caller
doesn't need to do anything in order to have the "safe" behaviour of
aborting as soon as an error occurs
3) If the operation *does* fail (and an exception is thrown) the
exception can give far richer detail than a simple true/false could
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
So in which case would I use second approach?
"Jon Skeet [C# MVP]" wrote:
Bruce One <ra**@virtualso ftware.com.brwr ote:
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:
<snip>
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??
The first approach is definitely better. It means:
1) The caller only has to do one thing in order to do what they want -
get the converted value.
2) If you use exceptions to indicate unexpected failures, the caller
doesn't need to do anything in order to have the "safe" behaviour of
aborting as soon as an error occurs
3) If the operation *does* fail (and an exception is thrown) the
exception can give far richer detail than a simple true/false could
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Bruce One <ra**@virtualso ftware.com.brwr ote:
So in which case would I use second approach?
Where the answer genuinely isn't useful at this point - in other words,
pretty rarely. Alternatively, "the answer" may not be a meaningful
concept, if many parts of the state of the object change.
If you have a situation where you wish to indicate success/failure
without using an exception, by the way, you should usually use the same
pattern as Double.TryParse , with an output parameter.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Bruce One <ra**@virtualso ftware.com.brwr ote:
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...
Have you considered writing a struct and using operator overloads to get
this functionality, rather than holding values in 'double'? (Just an
idea.)
Also, doubles aren't suitable for money anyway, due to rounding issues -
you should at least use 'decimal' instead.
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;
}
This method doesn't require state, so it can be a static method, in
which case your class is simply a namespace for global functions.
2) I create the method like this:
bool ConvertValue (double OriginalValue) {
// here I load private attribute ConvertedValue
ConvertedValue = ComputedValue
return OK;
}
I don't understand this. You are changing a private attribute, but what
does the containing class represent? If it represents a currency-typed
value, then why isn't the argument of the containing class type?
Also, I think if you have a problem, you should throw an exception,
rather than using a boolean return value to indicate success or failure.
If failure is to be expected sometimes, then provide two versions,
similar to Int32.Try/Parse and Dictionary<TKey ,TValue>.TryGet Value etc.
What is the best approach? Are there special situations in which we should
use one or another??
If you create a type to represent a currency value - i.e. create some
Currency class or struct - then I recommend that you make it immutable
and add operator overloads. That way leads to minimal confusion,
especially in the case of structs, where mutable methods can often end
up mutating copies rather than the originals.
If you're going to create a bunch of functions, then it's up to you to
get your semantics right everywhere you're writing code that manipulates
currency values.
Personally, I'd think hard about writing a Currency struct that is
'typed' - it keeps a tag indicating which currency its value refers to.
That way you have a tool with which to detect currency errors, at
runtime at least. And I'd overload it so that it works in a similar way
to 'decimal' (System.Decimal ) - i.e. I'd prevent doubles from being
valid currency values, and I'd require explicit typecasts in order for
it to be converted into a double.
-- Barry
-- http://barrkel.blogspot.com/
Well, I thought of using second approach for the simple reason it might be
"more OOP", otherwise why would properties exist for situations where you are
not dealing with visual objetcs?
Besides, isnt kind of bad habit working with "ref parameters" ?
"Jon Skeet [C# MVP]" wrote:
Bruce One <ra**@virtualso ftware.com.brwr ote:
So in which case would I use second approach?
Where the answer genuinely isn't useful at this point - in other words,
pretty rarely. Alternatively, "the answer" may not be a meaningful
concept, if many parts of the state of the object change.
If you have a situation where you wish to indicate success/failure
without using an exception, by the way, you should usually use the same
pattern as Double.TryParse , with an output parameter.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Bruce One <ra**@virtualso ftware.com.brwr ote:
Well, I thought of using second approach for the simple reason it might be
"more OOP",
A 'more OOP' approach would be the Currency struct approach I suggest in
a parallel post.
Besides, isnt kind of bad habit working with "ref parameters" ?
It's the only way to return multiple values without an extra allocation.
-- Barry
-- http://barrkel.blogspot.com/
Bruce One threw the animals some meat:
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...
double ConvertValue (double OriginalValue) {
// here I compute the value
return ComputedValue;
}
bool ConvertValue (double OriginalValue) {
// here I load private attribute ConvertedValue
ConvertedValue = ComputedValue
return OK;
}
What is the best approach? Are there special situations in which we should
use one or another??
There is more than one thing a Currency class might do. Your spec
focuses on conversions, like dollar to euro or euro to pound. In this
case, a suite of static methods is what you want.
You might also want a Currency class to represent a sort of
"globalized value" - a money data type that you can set as dollars and
read as pounds, or set as euros and read as rand, and so on. In this
case, you want read/write instance properties named things like
AsDollar or AsEuro. (You might want to account for transaction costs -
a larger factor on small transactions than on large transactions.)
One class can fill both roles. A separated Money instance might use
static methods of the ExchangeRates class for conversions, but many
..NET classes have static methods as well as instance methods and
instance properties. That is, a Currency class can have both static
methods and instance properties. You can have Currency instances that
represent values and Currency methods that manipulate values in just
the same way that the String class does.
--
..NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
Delphi skills make .NET easy to learn In print, in stores.
I must say using properties is far more elegant then using static methods...
But 1 thing I see that really makes a dif, 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).
The problem is that I havent found where to really use them. If properties
could be "replicated " using static methods returning values, then all my
business logic classes would be a set of static methods...
One thing I realized properties might be welcome is 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...
What do you think?
"Jon Shemitz" wrote:
Bruce One threw the animals some meat:
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...
double ConvertValue (double OriginalValue) {
// here I compute the value
return ComputedValue;
}
bool ConvertValue (double OriginalValue) {
// here I load private attribute ConvertedValue
ConvertedValue = ComputedValue
return OK;
}
What is the best approach? Are there special situations in which we should
use one or another??
There is more than one thing a Currency class might do. Your spec
focuses on conversions, like dollar to euro or euro to pound. In this
case, a suite of static methods is what you want.
You might also want a Currency class to represent a sort of
"globalized value" - a money data type that you can set as dollars and
read as pounds, or set as euros and read as rand, and so on. In this
case, you want read/write instance properties named things like
AsDollar or AsEuro. (You might want to account for transaction costs -
a larger factor on small transactions than on large transactions.)
One class can fill both roles. A separated Money instance might use
static methods of the ExchangeRates class for conversions, but many
..NET classes have static methods as well as instance methods and
instance properties. That is, a Currency class can have both static
methods and instance properties. You can have Currency instances that
represent values and Currency methods that manipulate values in just
the same way that the String class does.
--
..NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
Delphi skills make .NET easy to learn In print, in stores.
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??
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
by: s_m_b |
last post by:
function saveState() {
document.write (" | <a href = 'myhomepage.asp?
view=mhp&action=save&pb=");
>> document.write (document.phone.style.display.value);
document.write ("'>save view</a>");
};
creates an error at this (>) line.
|
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.
|
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
|
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
| |
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?
|
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.
|
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:...
|
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,
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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...
| |
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,...
|
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...
|
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...
|
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();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |