I have this class:
public class CalendarData_Day
{
public DateTime Date { get; set; }
public DayType TypeOfDay { get; set; }
public bool Choose { get; set; }
public CalendarData_Day(DateTime datum) : this(datum, DayType.Normal, true)
{
}
public CalendarData_Day(DateTime datum, DayType typDne) : this(datum, typDne, true)
{
}
public CalendarData_Day(DateTime datum, DayType typDne, bool vybran)
{
this.Date = datum;
this.TypeOfDay = typDne;
this.Choose = vybran;
}
}
and I want in second constructor check if DayType is Weekend and if it is then not send to Choose true but false. Anybody knows how can I do it?
I know I can add to last constructor if and checked but it doesn´t seem right for me. I think there is better way I think that I should do it other way or is this in last contructor okay:
if (TypeOfDay == DayType.Weekend)
this.Choose = false;
I know it´s working but I don´t know it is right way.
Edit:
I am sorry for that I don´t explained everything. There is more than 2 DayTypes, lets say there is Holiday, Work, … And I want that user can call class with just second constructor and if DayType would be Weekend or Holiday then Choose must be false but if it would be Normal or Work it should be true or user must user last contructor and set DayType to Work and Choose to false. It is complicated I am sorry I should wrote this first time.
It would be nicer to pass the chained constructor argument based on the parameter:
That way you don’t need to set the property twice – once to a sort of default value and then fix it based on information you already knew.
I would personally change the parameter name from
typDnetodayTypeor something similar.EDIT: I’ve only just seen that you were considering putting your test into the last constructor rather than the second one. I would expect the value given by the caller for
vybranto be accepted as-is, rather than conditionally ignored. You only describe wanting the second constructor to check forDayType == Weekend– not the last constructor – so it’s only the second constructor that should change.EDIT: If
Choosemust be false for Weekend or Holiday then I would enforce that in the last constructor but pick the value in the second constructor: