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:
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";
}
}
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?
– Maniero
OK I’ll edit the text
– Antonio Carlos Araújo
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.
– Maniero
I inserted an image with the error, this is what happens when I put to print what I want.
– Antonio Carlos Araújo
Now it’s easy, the interpolation solution is better and solves this problem.
– Maniero
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 =/.
– Antonio Carlos Araújo
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.
– Maniero
I’ll put it here then
– Antonio Carlos Araújo
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.
– Maniero
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.
– Maniero
I’ll take a look, thanks =)
– Antonio Carlos Araújo