Operator error can not be applied to method group

3

I'm trying to print a subtraction of dates using C # with WebForms, but I'm not sure how to correctly enter the attributes in the methods or how to print the method.

I'm converting the subtraction of the dates into days because I do not know how to work right with DateTime , I do not know if I can do this directly.

After converting the difference between the least date and the largest date into days I enter those days into a new DateTime which I called the total. So I have to print the total in days, months and years separately. But when I call in the Print method, which is called ImprimirExperiênciasTravalho() , the IDE complains that this operation is not allowed.

The result I expected from this class would be the impression of the company, the position, the description of the obligations of the person in the company and when the person enters the date of entry into the company and exit, this impression would show the total time the person worked in this company.

The error you are giving is this one:

  

Operator '+' can not be applied to operand od type 'strig' and 'method group'

The following is the C # code:

public class Experiencias_Trabalho
{
    private string nomeEmpresa, cargo, descricaoFuncao;
    private DateTime dataInicio = new DateTime();
    private DateTime dataSaida = new DateTime();

    public Experiencias_Trabalho(string nomeEmpresa, string cargo, string descricaoFuncao, DateTime dataInicio, DateTime dataSaida)
    {
        this.nomeEmpresa = nomeEmpresa;
        this.cargo = cargo;
        this.descricaoFuncao = descricaoFuncao;
        this.DataInicio = dataInicio;
        this.DataSaida = dataSaida;
    }

    public string NomeEmpresa
    {
        get
        {
            return nomeEmpresa;
        }

        set
        {
            nomeEmpresa = value;
        }
    }

    public string Cargo
    {
        get
        {
            return cargo;
        }

        set
        {
            cargo = value;
        }
    }

    public string DescricaoFuncao
    {
        get
        {
            return descricaoFuncao;
        }

        set
        {
            descricaoFuncao = value;
        }
    }

    public DateTime DataInicio
    {
        get
        {
            return dataInicio;
        }

        set
        {
            dataInicio = value;
        }
    }

    public DateTime DataSaida
    {
        get
        {
            return dataSaida;
        }

        set
        {
            dataSaida = value;
        }
    }

    public static string TempoTotal(DateTime DataInicio, DateTime DataSaida)
    {
        System.TimeSpan diff = DataSaida - DataInicio;
        int diasTotais = Convert.ToInt32(diff.TotalDays);
        DateTime total = new DateTime().AddDays(diasTotais);
        return (total.Year - 1) + " anos, " + (total.Month % 12) + " meses " + (total.Day % 365) + " dias.";
    }

    public string Imprimir_Experiencias_Trabalho()
    {
        return "<tr>"
                    + "<td>" + NomeEmpresa + "</td>"
                    + "<td>" + Cargo + "</td>"
                    + "<td>" + DescricaoFuncao + "</td>"
                    + "<td>" + TempoTotal + "</td>"
                    + "<td>" + "<i class='fa fa-edit'></i> <i class='fa fa-trash'></i>" + "</td>"
             + "</tr";
    }
}
    
asked by anonymous 23.04.2016 / 19:57

2 answers

5

There are several problems with this code. I'll fix some.

The TempoTotal() method does not have to be static, there is no gain in being, on the contrary, it is causing a problem.

It's much easier to create automatic properties and use them as a whole in code. See below for the huge simplification.

It's best to use string interpolation (well, this only works in C # 6 up). In this case there is no need to initialize the fields, but if it were it could be done right on the property.

Note how the code of the method in question may be simpler:

public class Experiencias_Trabalho {

    public Experiencias_Trabalho(string nomeEmpresa, string cargo, string descricaoFuncao, DateTime dataInicio, DateTime dataSaida) {
        NomeEmpresa = nomeEmpresa;
        Cargo = cargo;
        DescricaoFuncao = descricaoFuncao;
        DataInicio = dataInicio;
        DataSaida = dataSaida;
    }

    public string NomeEmpresa { get; set; }

    public string Cargo { get; set; }

    public string DescricaoFuncao { get; set; }

    public DateTime DataInicio { get; set; }

    public DateTime DataSaida { get; set; }

    public string TempoTotal() {
        var total = new DateTime().AddDays((int)(DataSaida - DataInicio).TotalDays);
        return (total.Year - 1) + " anos, " + (total.Month % 12) + " meses " + (total.Day % 365) + " dias.";
    }

    public string Imprimir_Experiencias_Trabalho() {
        return $@"<tr>
                      <td>{NomeEmpresa}</td>
                      <td>{Cargo}</td>
                      <td>{DescricaoFuncao}</td>
                      <td>{TempoTotal()}</td>
                      <td><i class='fa fa-edit'></i> <i class='fa fa-trash'></i></td>
                  </tr>";
    }
}

If you are not using C # 5 or earlier (I do not recommend), you can still simplify the printing method a bit. You can not use $ of interpolation, but you can use @ of verbatim.

Ideally a class should not have an underline following the C # style guide . Just as if the TempoTotal() method had the parameters, they should be named in lowercase.

    
23.04.2016 / 20:26
4

The problem is that Imprimir_Experiencias_Trabalho tries to concatenate with TempoTotal . TempoTotal is a method. For this reason you can call it as follows:

TempoTotal(DataInicio, DataSaida);
    
23.04.2016 / 20:21