Operator error cannot be applied to the method group

Asked

Viewed 837 times

3

I’m trying to print a date subtraction using C# with Webforms, but I don’t know how to correctly insert attributes in methods or how to print the method.

I’m converting the subtraction of dates into days because I don’t know how to work right with the DateTime, I don’t know if you can do it directly.

After converting the difference between the date less and the date greater in days I insert those days into a new DateTime which I called total. Then I have the total printed in days, months and years separately. Only when I call in the method of Impression that is called ImprimirExperiênciasTravalho() the IDE accuses that this operation is not allowed.

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

The mistake you’re making is this one: Imagem do Erro

Operator '+' cannot be Applied to operand od type 'strig' and 'method group'

Below is code C#:

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";
    }
}
  • 1

    You just said you’re having problems. Specify the difficulty you’re having. It’s all too general. What’s not working? What is the expected result?

  • OK I’ll edit the text

  • Not because you don’t detail this error. Without complete information it is difficult to help. No one here is seeing what you are seeing. I will rewrite the code to make it simpler and in the style of C#, but without more information I have no way to fix any additional error that is not described in the question or that is very apparent.

  • I inserted an image with the error, this is what happens when I put to print what I want.

  • 1

    Now it’s easy, the interpolation solution is better and solves this problem.

  • Thanks again, I am since 10 am trying to solve this, I only came here because I had read the bilbiotecas of C# and I did not understand how to fix it. I wanted to know where you learn the syntax so well because in my college I find the explanations of the syntax of the languages very shallow, it ends up slowing us down in these mistakes =/.

  • 1

    It would still be better to put in the question the error description as text to help other people with the same error find the solution here when googlare.

  • I’ll put it here then

  • 1

    College is a place to teach the general concept of computing, not to teach syntax. This is learned by studying on its own and by training.But without the base, and this the good college has to give, it becomes difficult to study. Memorizing syntax is not important, understand well how things work, because it is what works. Then you can manage alone. If memorize syntax will always depend on someone to solve the problems for you. No problem with that when you’re learning, but it’s a big problem if you’re "experienced" and still need other people for something simple.

  • 1

    If you want to learn more about C# in several ways, one of them is to read the questions here: http://answall.com/questions/tagged/c%23? Sort=votes&pageSize=50 Mine are usually some of the most instructive (I like to teach more than to solve a person’s problem, so they don’t always have to come back, I don’t like a captive :) ): http://answall.com/search?tab=votes&q=user%3a101%20[c%23] Don’t forget to vote for everything you find useful.

  • I’ll take a look, thanks =)

Show 6 more comments

2 answers

5


There are several problems in this code. I will fix some.

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

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

You better use interpolation of string (well, this only works in C# 6 up). In this case there is no need to initialize the fields, but if it was could do right in the property.

Note how the code of the method in question can 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>";
    }
}

I put in the Github for future reference.

If you are not using C# 5 or earlier (I do not advise), you can still simplify the printing method a little. You cannot use $ interpolation, but you can use the @ Verbatim.

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

  • Thanks for the help, I’ll try to arrange it here this way to see if it gets better, thanks Big.

  • I wanted to leave a tip bigown, when the examples are short, as was the case of OP, a very useful tool to determine where the error is csharppad. I even went to ask error but then I remembered this tool and removed the comment.

  • @Brunocosta legal. But anyway it gets much more organized the person put the error so later other people with the same error can find the solution.

4

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

TempoTotal(DataInicio, DataSaida);
  • The bug is gone, it seems that this is what I needed to do. Thanks. Now I will arrange the modeling to receive this information. If the printing of the data is correct I mark your answer here as a solution. Vlw msm

Browser other questions tagged

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