CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2004
    Posts
    429

    Question Converting a String to Integer [C#]

    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?

  2. #2
    Join Date
    Nov 2004
    Location
    Poland
    Posts
    1,355

    Re: Converting a String to Integer [C#]

    try:
    dtSelected.Minute = Convert.ToInt32(ddlMinute.SelectedValue);

  3. #3
    Join Date
    Oct 2004
    Posts
    429

    Question Re: Converting a String to Integer [C#]

    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?

  4. #4
    Join Date
    Nov 2004
    Location
    Poland
    Posts
    1,355

    Re: Converting a String to Integer [C#]

    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
    );

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured