Click to See Complete Forum and Search --> : Converting a String to Integer [C#]


Shaitan00
February 1st, 2005, 02:15 AM
Given a DropDownList box [ddlHour] and a DateTime variable [dtSelected].
I want to update the hour in dtSelected.hour with the ddlHour.SelectedValue however when I try to do the following directly:
dtSelected.Minute = ddlMinute.SelectedValue;

I get the following error:
1) Cannot implicitly convert type 'string' to 'int'
2) Property or indexer 'System.DateTime.Hour' cannot be assigned to -- it is read only

So I guess I have 2 questions; how could I convert the string returned by ddlHour.SelectedValue into an Integer so I can update the dtSelected.hour AND why am I seeing that read-only error? Am I not doing this right at all?

Krzemo
February 1st, 2005, 02:30 AM
try:
dtSelected.Minute = Convert.ToInt32(ddlMinute.SelectedValue);

Shaitan00
February 7th, 2005, 02:16 AM
When using the following code:
dtSelected.Minute = Convert.ToInt32(ddlMinute.SelectedValue);

I get the following error:
Property or indexer 'System.DateTime.Minute' cannot be assigned to -- it is read only

Can I turn read-only off?

Krzemo
February 7th, 2005, 02:32 AM
Hmm dtSelected is a DateTime value?.....

Than why U assigned values to read only fields?
Rather Use apropriate constructor.
For example:


dtSelected=new System.DateTime(1979, // Year
07, // Month
28, // Day
22, // Hour
35, // Minute
5, // Second
15, // Millisecond
calendar // Calendar
);