Click here to Skip to main content
12,451,227 members (54,081 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

8.8K views
1 bookmarked
Posted

How to perform arithmetic operations between any objects by inheriting a common arithmetic parent class in .NET 4.

, 8 Apr 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
You got me thinking about other ways to accomplish "objects that are not numbers honoring math operators without implementing the operators on the objects."I'm too long in the tooth of strongly typed languages to have a good comfort level with all things dynamic (although reading about the...
You got me thinking about other ways to accomplish "objects that are not numbers honoring math operators without implementing the operators on the objects."

I'm too long in the tooth of strongly typed languages to have a good comfort level with all things dynamic (although reading about the BinaryOperationBinder has raised my curiosity level!), so I thought I'd look for a non-dynamic way to accomplish your goal. It will also help readers who are not able to use .NET 4.0.

I first tried to use extension methods, but it seems you can't implement an operator as an extension method.

But... you can implement casts between objects and numeric types. So, here is a solution that allows non-numeric objects to cleanly participate in numeric expressions:

namespace ObjectMathOperations
    {
    class Program
        {
        static void Main(string[] args)
            {
            ScorableSubject math = 95.3;
            ScorableSubject science = 97;
            double totalScore = math + science;
            }
        }
 
    public class ScorableSubject
        {
        public double MarksScored { get; set; }
        public static implicit operator double(ScorableSubject from)
            {
            return from.MarksScored;
            }
        public static implicit operator ScorableSubject(double from)
            {
            return new ScorableSubject { MarksScored = from };
            }
        }
    }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Chris Trelawny-Ross
Software Developer (Senior) Baylor Healthcare System
United States United States
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralRe: "By assigning it to a double you are deliberately saying "ge... Pin
Albin Abel25-Apr-11 7:22
memberAlbin Abel25-Apr-11 7:22 
GeneralRe: If you want to preserve the other properties of sub1, you sh... Pin
Chris Trelawny-Ross25-Apr-11 6:42
memberChris Trelawny-Ross25-Apr-11 6:42 
GeneralRe: Let us say this is the class. public class ScorableSubj... Pin
Albin Abel25-Apr-11 6:34
memberAlbin Abel25-Apr-11 6:34 
GeneralRe: Hmmm. Ok. Thanks for telling. :thumbsup: Pin
Sandeep Mewara24-Apr-11 23:05
mvpSandeep Mewara24-Apr-11 23:05 
GeneralHi I was trying to use this solution but then found a very p... Pin
Albin Abel24-Apr-11 22:54
memberAlbin Abel24-Apr-11 22:54 
GeneralRe: Virtually upvoted it. :) Pin
Sandeep Mewara24-Apr-11 23:06
mvpSandeep Mewara24-Apr-11 23:06 
GeneralRe: Albin, I'm not sure what I violated: my object is still str... Pin
Chris Trelawny-Ross25-Apr-11 4:35
memberChris Trelawny-Ross25-Apr-11 4:35 
GeneralReason for my vote of 5 Excellent refactoring to a lucid exp... Pin
MacMaverick10-Apr-11 22:18
memberMacMaverick10-Apr-11 22:18 
GeneralRe: Hi MacMaverick this solution has a problem which is not advi... Pin
Albin Abel24-Apr-11 22:56
memberAlbin Abel24-Apr-11 22:56 
GeneralReason for my vote of 5 Nice Idea! My 5 Pin
RaviRanjankr6-Apr-11 9:58
mvpRaviRanjankr6-Apr-11 9:58 
GeneralRe: Hi RaviRnjankr this solution has a problem which is not advi... Pin
Albin Abel24-Apr-11 22:57
memberAlbin Abel24-Apr-11 22:57 
Generalmy 5 Pin
Albin Abel6-Apr-11 7:24
memberAlbin Abel6-Apr-11 7:24 
GeneralGood one. Pin
Sandeep Mewara6-Apr-11 0:51
mvpSandeep Mewara6-Apr-11 0:51 
GeneralRe: Hi Sandeep this solution has a problem which is not advisabl... Pin
Albin Abel24-Apr-11 22:57
memberAlbin Abel24-Apr-11 22:57 
GeneralGood idea. Pin
Albin Abel5-Apr-11 17:11
memberAlbin Abel5-Apr-11 17:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160826.1 | Last Updated 8 Apr 2011
Article Copyright 2011 by Chris Trelawny-Ross
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid