Click here to Skip to main content
15,938,325 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
8 Apr 2011CPOL 21.3K   1   15
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)


Written By
Software Developer (Senior) Baylor Healthcare System
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: "By assigning it to a double you are deliberately saying "ge... Pin
Albin Abel25-Apr-11 7:22
Albin 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
Chris Trelawny-Ross25-Apr-11 6:42 
GeneralRe: Let us say this is the class. public class ScorableSubj... Pin
Albin Abel25-Apr-11 6:34
Albin Abel25-Apr-11 6:34 
GeneralRe: Hmmm. Ok. Thanks for telling. :thumbsup: Pin
Sandeep Mewara24-Apr-11 23:05
mveSandeep 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
Albin Abel24-Apr-11 22:54 
GeneralRe: Virtually upvoted it. :) Pin
Sandeep Mewara24-Apr-11 23:06
mveSandeep 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
Chris Trelawny-Ross25-Apr-11 4:35 
GeneralReason for my vote of 5 Excellent refactoring to a lucid exp... Pin
MacMaverick10-Apr-11 22:18
MacMaverick10-Apr-11 22:18 
GeneralRe: Hi MacMaverick this solution has a problem which is not advi... Pin
Albin Abel24-Apr-11 22:56
Albin Abel24-Apr-11 22:56 
GeneralReason for my vote of 5 Nice Idea! My 5 Pin
RaviRanjanKr6-Apr-11 9:58
professionalRaviRanjanKr6-Apr-11 9:58 
GeneralRe: Hi RaviRnjankr this solution has a problem which is not advi... Pin
Albin Abel24-Apr-11 22:57
Albin Abel24-Apr-11 22:57 
Generalmy 5 Pin
Albin Abel6-Apr-11 7:24
Albin Abel6-Apr-11 7:24 
GeneralGood one. Pin
Sandeep Mewara6-Apr-11 0:51
mveSandeep Mewara6-Apr-11 0:51 
GeneralRe: Hi Sandeep this solution has a problem which is not advisabl... Pin
Albin Abel24-Apr-11 22:57
Albin Abel24-Apr-11 22:57 
GeneralGood idea. Pin
Albin Abel5-Apr-11 17:11
Albin 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.