1

I am trying to check an entered time in a DateTimePicker control and correct it to the closest acceptable value in C#

if (Summer1.Value.TimeOfDay > Summer2.Value.TimeOfDay)
                Summer1.Value.TimeOfDay = Summer2.Value.TimeOfDay;

It gives me the following:

Property or indexer 'System.DateTime.TimeOfDay' cannot be assigned to -- it is read only

Why does it handle the DateTimePicker object as a system time? I could not find anything related to Read-Only state of the control in VS 2003.

Oded
  • 489,969
  • 99
  • 883
  • 1,009

1 Answers1

1

Summer1.Value is the property of DateTimePicker and it has type of DateTime. This property is assignable.

You're trying to assign Summer1.Value.TimeOfDay - which is a property of DateTime object Summer1.Value - and this property does not have setter in DateTime class.

Summer1.Value = Summer2.Value

should work fine but may not give you the desired result.

The closest to your desirable output will be something like

Summer1.Value = Summer1.Value.Date + Summer2.Value.TimeOfDay;
Sergey Kudriavtsev
  • 10,328
  • 4
  • 43
  • 68