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
I don’t understand what the problem is.
– Maniero
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.– Jéf Bueno
@bigown, the Datetime.Tryparse takes a string ("276,11") and converts to a date.
– Marco Souza
@jefersonb, there’s no way to verify this?
– Marco Souza
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?– Jéf Bueno
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.
– Marco Souza