locked
Can't Convert Double to Timespan RRS feed

  • Question

  • Hi Currently I am working on my code in a loop:

    for(int iterator = 0; iterator< laptimes.Count; iterator++)

    {

    avlaptime = (laptimes[i] - laptimes[i - 6]).TotalSeconds / 8;

    }

    currently i am getting a "cannot implicitly convert type 'double' to 'System.TimeSpan' for some reason, can anyone recommend a solution, For info laptimes is a list of DateTime.


    Wednesday, February 26, 2014 9:10 PM

Answers

  • " i am getting a "cannot implicitly convert type 'double' to 'System.TimeSpan' for some reason"

    The reason is that the right hand side is a double (TimeSpan - TimeSpa) is a TimeSpan, then TotalSeconds is a double and divide by int is a double) but the left hand side is a TimeSpan.  There is not implicit conversion from double to TimeSpan.

    There is a static method on the TimeSpan class which will create a TimeSpan given a number of seconds as a double

    avlaptime = TimeSpan.FromSeconds(yourOriginalRightHandSide);


    Paul Linton

    • Proposed as answer by Arno Brouwer Tuesday, March 4, 2014 3:48 PM
    • Marked as answer by Eason_H Thursday, March 6, 2014 1:24 AM
    Wednesday, February 26, 2014 10:34 PM

All replies

  • Is avlaptime a TimeSpan?

    The expression (laptimes[i] - laptimes[i - 6]).TotalSeconds / 8 will return a double as TotalSeconds property is a double, assuming laptimes is an array with DateTime.

    You probably need to switch the type of avlaptime to double.

    Wednesday, February 26, 2014 9:27 PM
  • Hi avlaptime is a TimeSpan currently
    Wednesday, February 26, 2014 9:31 PM
  • my bad too as it is[i-8] not 6
    Wednesday, February 26, 2014 9:32 PM
  • Hi avlaptime is a TimeSpan currently
    You will need to change it to double as the calculation you make will return a double.
    Wednesday, February 26, 2014 10:07 PM
  • " i am getting a "cannot implicitly convert type 'double' to 'System.TimeSpan' for some reason"

    The reason is that the right hand side is a double (TimeSpan - TimeSpa) is a TimeSpan, then TotalSeconds is a double and divide by int is a double) but the left hand side is a TimeSpan.  There is not implicit conversion from double to TimeSpan.

    There is a static method on the TimeSpan class which will create a TimeSpan given a number of seconds as a double

    avlaptime = TimeSpan.FromSeconds(yourOriginalRightHandSide);


    Paul Linton

    • Proposed as answer by Arno Brouwer Tuesday, March 4, 2014 3:48 PM
    • Marked as answer by Eason_H Thursday, March 6, 2014 1:24 AM
    Wednesday, February 26, 2014 10:34 PM
  • Yes ! That's right it seems to be okay now I'm not getting any Errors :)
    Thursday, February 27, 2014 11:15 AM