Converting a String to Integer [C#]

csharp United States
  • 16 years ago

    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?

  • 16 years ago

    to convert data types, there is the Convert keyword, use like

    int Value1 = 0;
    string Value2 = Convert.toString(Value1);

    FOR 2 'I want to update the hour in dtSelected.hour' Can you show the code you use, thanks

  • 16 years ago

    1) for the type conversion, you'll need to do
    dtSelected.Minute = Convert.ToInt32(ddlMinute.SelectedValue)

    2) For the problem with assignment - you'll have to create a new DateTime object - using the values from the existing DateTime object, or use the AddHours/AddMinutes etc methods, if that's appropriate.

Post a reply

Enter your message below

Sign in or Join us (it's free).


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Brevity is the soul of wit” - Shakespeare