Weltkind
Weltkind

Reputation: 1

Overloading >= operator

I need to overload operator >=. If the condition is true, the operator returns true, otherwise false. If at least one of the objects is null – throw an exception (ArgumentException). I tried this. What's wrong?

public static bool operator false(Staff inputPerson)
{
    if ((inputPerson.salary) <= 15000)
    {
        return true;
    }
    else if ((inputPerson.salary) is null)
    {
        throw new ArgumentOutOfRangeException("This person does not have a job");
    }

    return false;
}

Upvotes: 0

Views: 108

Answers (2)

stevethethread
stevethethread

Reputation: 2524

You need to do something like public static bool operator <= (Rational rational1, Rational rational2)

When you overload this, you need to ensure that you overload all the related operators too. e.g. <, > <=, >= etc. as well as the equality operators and methods.

You need to pass in both of the objects to be compared, as the method is static, and not an instance method.

Upvotes: 3

BCartolo
BCartolo

Reputation: 719

Try this:

public static bool operator >=(Staff p1, Staff p2) {
    if (p1 is null || p2 is null) {
        throw new ArgumentOutOfRangeException("This person does not have a job");
    }
    return p1.salary >= p2.salary;
}

Source: http://msdn.microsoft.com/en-us/library/aa288467%28v=vs.71%29.aspx

Upvotes: 0

Related Questions