Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / November 2006

Tip: Looking for answers? Try searching our database.

Addition of two values

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ggeshev - 23 Nov 2006 15:13 GMT
Hello!

I've got a method :
public static Object _Add(Object val1, Object val2); {}

val1 and val2 are among of these types :
- sbyte
- byte
- short
- ushort
- int
- uint
- long
- ulong
- float
- double
- decimal

Types of val1 and val2 do not necessarily match (i.e. val1 may be int and
val2 may be decimal).
The method should implement the Addition operation (val1 + val2) and return
the result in the propper type (i.e. if an integer and double are added,
result should be "double"; if sbyte and sbyte are added, the result should
be sbyte or short - if the resulting value is beyond sbyte limits).

What would be the implementation of this method look like?
Propose me some ideas for implementing the Addition operation and returning
the result in a propper type.

Thank you!
Jon Skeet [C# MVP] - 23 Nov 2006 15:27 GMT
> I've got a method :
> public static Object _Add(Object val1, Object val2); {}
[quoted text clipped - 22 lines]
> Propose me some ideas for implementing the Addition operation and returning
> the result in a propper type.

There are various other constraints you should think about:

1) If you are passed two ints, should the result always be an int, or
could it overflow into a long?
2) If you are passed (int)1 and (int)1 could the result actually be
(byte)2?
3) What would you expect to happen with decimal + double where you may
not be able to preserve both scale and precision within one value?
4) How do you expect the caller to cope with all of this anyway?

Jon
ggeshev - 23 Nov 2006 15:34 GMT
> 1) If you are passed two ints, should the result always be an int, or
> could it overflow into a long?
[quoted text clipped - 5 lines]
>
> Jon

1) The result should be long
2) The result should be int
3) The result should be double
Jon Skeet [C# MVP] - 23 Nov 2006 16:33 GMT
> > 1) If you are passed two ints, should the result always be an int, or
> > could it overflow into a long?
[quoted text clipped - 7 lines]
> 2) The result should be int
> 3) The result should be double

Okay. Thinking about unsigned types as well, I think there are *lots*
of combinations you should consider.

I would start by writing a *long* set of unit tests - as many odd (and
normal!) combinations as you can think of. Each test should be
incredibly simple - just assert that the result of the addition is what
you want it to be.

Once you've got that set of unit tests, it'll be a lot easier to start
implementing the production code - you'll have thought of a lot of the
issues involved while writing the tests, and recorded the answers at
the same time. You'll probably want to start off the implementation by
working out whether you're dealing with integer arithmetic or floating
point, and possibly have one private method for each, just to keep
things clean.

Jon
ggeshev - 24 Nov 2006 07:48 GMT
> Okay. Thinking about unsigned types as well, I think there are *lots*
> of combinations you should consider.
[quoted text clipped - 13 lines]
>
> Jon

Thank you Jon!
Thank you for your proposals!
I was wondering if I'm missing some very easy and tricky way of achieving my
aim by casting the input Object values /val1 and val2/ to certain Interfaces
for example, or using reflection probably.
Jon Skeet [C# MVP] - 24 Nov 2006 19:17 GMT
<snip>

> I was wondering if I'm missing some very easy and tricky way of achieving my
> aim by casting the input Object values /val1 and val2/ to certain Interfaces
> for example, or using reflection probably.

No, I don't believe so.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.