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:
public CalendarData_Day(DateTime datum, DayType typDne)
: this(datum, typDne, typeDne != DayType.Weekend)
{
}
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 typDne
to dayType
or 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 vybran
to be accepted as-is, rather than conditionally ignored. You only describe wanting the second constructor to check for DayType == Weekend
- not the last constructor - so it's only the second constructor that should change.
EDIT: If Choose
must be false for Weekend or Holiday then I would enforce that in the last constructor but pick the value in the second constructor:
public CalendarData_Day(DateTime datum, DayType typDne)
: this(datum, typDne,
typeDne != DayType.Weekend && typeDne != DayType.Holiday)
{
}
public CalendarData_Day(DateTime datum, DayType typDne, bool vybran)
{
if (vybran && (typeDne == DayType.Weekend || typeDne == DayType.Holiday))
{
throw new ArgumentException(
"vybran cannot be true for holiday or weekend dates", "vybran");
}
this.Date = datum;
this.TypeOfDay = typDne;
this.Choose = vybran;
}
Validating parameters in your constructor is perfectly OK.
Though you should ensure that if the condition is false your code still works as expected:
if (TypeOfDay == DayType.Weekend)
{
this.Choose = false;
}
else
{
this.Choose = vybran;
}
You may consider throwing an ArgumentException
if the parameters passed in are completely wrong.
If you have very extensive logic in one of your constructors you might benefit from static construction functions calling private constructors.
class MyClass
{
private MyClass(...)
{
}
public static MyClass CreateMyClassWithValidation(...)
{
if(....)
return new MyClass(...);
}
}
精彩评论