Akshay J
Akshay J

Reputation: 5458

Check if some decimal values are equal

I am using the following line of code to check if the decimals are equal or not but its showing syntax error.

if (ProgramVariables.MSR_AR_System == ProgramVariables.MSR_AR_EB_1 == ProgramVariables.MSR_AR_EB_2 == ProgramVariables.MSR_AR_EB_3)

Whats the correct method ?

Upvotes: 1

Views: 483

Answers (6)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236228

You can write method like this:

public static bool AllEqual<T>(params T[] values)
    where T : struct
{
    if (values.Length < 2) 
        return true;

    T first = values[0];
    for (int i = 1; i < values.Length; i++)
    {
        if (!values[i].Equals(first))
            return false;
    }

    return true;
}

And use it to compare all values:

if (AllEqual(ProgramVariables.MSR_AR_System,
             ProgramVariables.MSR_AR_EB_1,
             ProgramVariables.MSR_AR_EB_2,
             ProgramVariables.MSR_AR_EB_3))

Upvotes: 0

D J
D J

Reputation: 7028

try this

if (ProgramVariables.MSR_AR_System.Equals(ProgramVariables.MSR_AR_EB_1).Equals(ProgramVariables.MSR_AR_EB_2.Equals(ProgramVariables.MSR_AR_EB_3)))

Upvotes: 0

aaa
aaa

Reputation: 1124

Comparison operator return boolean value, i.e. True or False. So first comparison returns boolean value and that you are comparing with decimal. Hence the error.

Upvotes: 0

mishmash
mishmash

Reputation: 4458

You cant just do A == B == C == D. You have to use the && (AND) operator, like so:

if (a == b && b == c && c == d && d == e)
{
    // Do something
}

Which means if a equals b AND b equals c AND c equals d AND d equals e then.

Why does this happen? Because the equality operator takes two arguments of the same type. a == b results in a boolean (true or false) and you compare this result to the next value of c which is still of type decimal and you cant compare a boolean to a decimal as they are not of the same type.

Upvotes: 1

Olaf Dietsche
Olaf Dietsche

Reputation: 74028

The == operator is a binary operator and is evaluated from left to right. This means, it evaluates first

ProgramVariables.MSR_AR_System == ProgramVariables.MSR_AR_EB_1

which gives a boolean value and then this boolean value is compared to

(true or false) == ProgramVariables.MSR_AR_EB_2

which again gives a boolean value and then this second boolean value is compared to

(true or false) == ProgramVariables.MSR_AR_EB_3

You get an error, because you compare values of incompatible types.

The correct way to compare multiple values is to combine them by the logical && (and) operator, for example

if (ProgramVariables.MSR_AR_System == ProgramVariables.MSR_AR_EB_1 
    && ProgramVariables.MSR_AR_System == ProgramVariables.MSR_AR_EB_2 
    && ProgramVariables.MSR_AR_System == ProgramVariables.MSR_AR_EB_3)

Upvotes: 0

cuongle
cuongle

Reputation: 75306

It should be:

if (ProgramVariables.MSR_AR_System == ProgramVariables.MSR_AR_EB_1 
 && ProgramVariables.MSR_AR_EB_1 == ProgramVariables.MSR_AR_EB_2 
 && ProgramVariables.MSR_AR_EB_2 == ProgramVariables.MSR_AR_EB_3)

Upvotes: 0

Related Questions