Tryparse error of Datetime converting string in Double format

Asked

Viewed 894 times

2

I have a method of using the DateTime.TryParse to convert a string for the guy DateTime, but lately I’ve realized that string format Double would also be being converted to date.

Is there any solution to fix this?

public class SaidaData : SaidaBase
{
    public SaidaData() { }
    public DateTime? Data { get; set; }
}

public class SaidaBase
{
    public SaidaBase() { }

    public Boolean Sucesso { get; set; }
    public String Mensagem { get; set; }
}

public class EntradaBase
{
    public EntradaBase() { }

    public String Conteudo { get; set; }

}

public class Entrada : EntradaBase
{
    public Entrada() { }

    public Int32 PosIni { get; set; }
    public Int32 Tamanho { get; set; }
    public String[] Array { get; set; }
}

#region ConverterCampoDateTime
public static SaidaData ConverterCampoDateTime(Entrada entrada)
{
    var saida = new SaidaData();

    DateTime valor;
    var convertido = DateTime.TryParse(entrada.Conteudo, out valor);

    if (convertido)
    {
        //saida.Mensagem = Resources.Mensagens.OK;
        saida.Mensagem = "OK";
        saida.Sucesso = true;
        saida.Data = valor;
    }
    else
    {
        //saida.Mensagem = Resources.Mensagens.Erro;
        saida.Data = null;
        saida.Mensagem = "ERRO";
        saida.Sucesso = false;
    }

    return saida;
}
#endregion

SS SS

  • I don’t understand what the problem is.

  • Who’s in charge of conversion is not the "format" of the input string and yes the class. If you are using DateTime.TryParse it is obvious that the conversion will be made to a date.

  • @bigown, the Datetime.Tryparse takes a string ("276,11") and converts to a date.

  • @jefersonb, there’s no way to verify this?

  • 1

    You can check it in your hand. Now explain something to me, you’re passing this content to a method called ConverterParaDateTime and want the conversion not to be done? Why call the method then?

  • This method already existed in the application, and with it also a check if it had been converted successfully and shown in the class according to the above code.

Show 1 more comment

1 answer

5


A method of parse analyzes a text and tries to find something he recognizes there. If he recognizes and can generate a result he will do it. He makes the best possible effort. If the intention is only to recognize a specific format you have to define this format, for that there is the TryParseExact(). Example (format, culture and style may have to be different):

DateTime.TryParseExact(entrada.Conteudo, "dd/MM/yyyy",
    CultureInfo.InvariantCulture, DateTimeStyles.None, out var valor);

I put in the Github for future reference.

Confusion may occur because the data of this class is confusing. Perhaps these classes are supporting too much.

I think it’s weird having this inheritance, but I don’t know the problem. I’m just giving a hint that the problem could be bigger and the architecture problematic, the wrong conversion is just the symptom.

If it’s to make a class like this, I even have my doubts if you need property.

These builders are unnecessary.

I don’t really like the use of . NET types in place of C#types. It’s taste, you can use it if you want, but it’s indicative of invention :)

The good part is you’re using a method Try.

  • Thanks for the tips, your solution solved the problem, this architecture is really not good, but I believe you already know how to work in a company with a strongly coupled system.

  • I know what it’s like ;)

Browser other questions tagged

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