How to allow a null date?

1

How can I assign a null date to a variable of the DateTime type?

I have the following code:

DateTime? FechaNula = new DateTime?();
deudaDto.FechaIngreso = deuda.FechaIngreso.HasValue ? deuda.FechaIngreso : FechaNula;

but you mark me error, says that you can not implicitly convert the System.DateTime type? in System.DateTime ....

Greetings and thanks

    
asked by Luis Gabriel Fabres 10.08.2017 в 19:34
source

2 answers

2

This code should work

DateTime FechaNula = new DateTime();
deudaDto.FechaIngreso = deuda.FechaIngreso.HasValue ? deuda.FechaIngreso.Value : FechaNula;

Apparently deudaDto.FechaIngreso is of type DateTime so it requires that the expression on the right is not null

A shorter way to achieve the same would be

deudaDto.FechaIngreso = deuda.FechaIngreso ?? new DateTime();

Which means that if FechaIngreso is not null that its value is assigned, otherwise it is assigned a new DateTime() (the "null" date)

    
answered by 10.08.2017 / 19:38
source
0

Because if you are going to assign a null date to Debt.DateInside, this must also be nullable, your property statement should look like this:

public DateTime? FechaIngreso {get; set;}
    
answered by 10.08.2017 в 19:40