Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override objects return value

Tags:

c#

I'm trying to compare an object with an int value such as

if (myObject - 5 == 0)
    doSomething();

my class could look something like this: (most setters/getters removed, so don't mind that all variables are private)

public class SomeClass
{
    public string name;
    private int minValue;
    private int maxValue;
    private int currValue;

    public int getCurrentValue()
    {
        return currValue;
    }
}

What I'm trying to achieve is something like this:

someClassInstance - 5;

to be equal

someClassInstance.getCurrentValue() - 5;

Can I make an override for the object to act as an int (it's own variable) opposed to just being an object?

like image 634
Creative Magic Avatar asked Jan 29 '14 08:01

Creative Magic


3 Answers

May be operator is the case?

public class SomeClass {
  ...

  public static int operator -(SomeClass left, int right) {
    if (Object.ReferenceEquals(null, left))
      throw new ArgumentNullException("left");

    return left.getCurrentValue() - right;
  }
}

...

SomeClass someClassInstance = new SomeClass(...);

int result = someClassInstance - 5;

Another possibility (based on implicit operator) is to convert SomeClass implicitly to int whenever required:

public class SomeClass {
  ...

  // Whenever int is requiered, but SomeClass exists make a conversion
  public static implicit operator int(SomeClass value) {
    if (Object.ReferenceEquals(null, value))
      throw new ArgumentNullException("value");

    return value.getCurrentValue();
  }
}

...

SomeClass someClassInstance = new SomeClass(...);

int result = someClassInstance - 5;
like image 136
Dmitry Bychenko Avatar answered Oct 19 '22 09:10

Dmitry Bychenko


Actually you would be much better off overriding operator int, that way you can do far more calculations with less overloads:

using System;

namespace Demo
{
    public class SomeClass
    {
        public string name;
        private int minValue;
        private int maxValue;
        public int currValue;

        public int getCurrentValue()
        {
            return currValue;
        }

        public static implicit operator int(SomeClass value)
        {
            if (value == null)
                throw new ArgumentNullException("value");

            return value.currValue;
        }
    }

    internal class Program
    {
        private void run()
        {
            var test = new SomeClass {currValue = 5};

            if (test - 5 == 0)
                Console.WriteLine("It worked");

            if (test + 5 == 10)
                Console.WriteLine("This also worked");
        }

        private static void Main()
        {
            new Program().run();
        }
    }
}
like image 45
Matthew Watson Avatar answered Oct 19 '22 09:10

Matthew Watson


You could experiment with a mixture of implicit conversions and operator overloading, but from my experience you will never make it work as seamlessly as you wish (and as you could get it to work in C++).

If I were you, I would change the getCurrentValue to a property:

public int CurrentValue
{
    get {return currValue};
}

and just use someClassInstance.CurrentValue -5

like image 1
Grzenio Avatar answered Oct 19 '22 10:10

Grzenio