Condition does not work in a Setter method

Asked

Viewed 81 times

2

Why does my code give the following error:

The operator '>' cannot be applied to type operands decimal and double.

Code:

class Funcionario
{
    public string nome { get; set; }
    public string sobrenome { get; set; }
    private decimal salario { get; set; }

    public Funcionario(string Nome, string Sobrenome, decimal Salario)
    {
        nome = Nome;
        sobrenome = Sobrenome;
        salario = Salario;
    }

    public void sal(decimal Salario)
    {
        if (Salario < 0 && Salario > 20.000)
        {
            throw new ArgumentOutOfRangeException("ArgumentOutOfRangeException");
        }
    }
}   

1 answer

2


Because 20.000 it is not what it imagines to be. If you want a literal of monetary value and so be able to compare with a type decimal should use then 20M. You don’t have to put zeros after the comma, but you might think it’s more readable, it’s still the same.

In addition it may not be the operator &&, it is impossible for something to be less than 0 and at the same time to be greater than 20, a || solve this, because it is one or the other.

Are you sure the salary should be less than 20 real? Or you wanted to do 20_000M? Can also use without separator: 20000M.

If it’s a Setter, then the correct one should use the name SetSalario()? But if it’s a Setter should not use a property? In fact already created one, because it needs a Setter, when you already have one, implement it. And use the correct nomenclature standard.

Even if you want to keep it that way Salario() to make it clear that this is not a food ingredient? Or at least use Sal() to use the pattern that is used in C#? And could match the correct nomenclature for the parameter as well.

The exception even makes sense in property, although it can be eliminated, but if it will use one method, wouldn’t it be better to use another mechanism? See Why should we avoid returning error codes? and It is good practice to make an exception in such cases?.

If you still insist on using an exception it would not be better to create a specific one that indicates what the error actually is and not use a ready one that indicates something completely different than what happens there?

Not understanding deeply what it is, for whom it serves, what is the sense of exceptions, wouldn’t it be better not to use? Using something wrong only trains error.

Anyway, try to write codes that make sense, avoid writing randomly, will give a much better result.

Used decimal for monetary value, but I think it was coincidence, the comments indicate this. See What is the correct way to use the float, double and decimal types?.

C# 9 can do all this in a better way, but I will not put here not to confuse more.

  • After changing && to || and 20,000 to 20,000 the error has ceased. Thank you! (Obs: I’m a little rusty on C# and for that reason I’m redoing some codes.)

  • The error stopped happening, but the code is still wrong, and I need to take the congratulations I gave, now there’s a problem that showed that used the decimal coincidentally.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.