Hello all,
I'm really struggling to find a way or method to convert my TimeSpan data type into an Int so I can put the value into an Array. Now I know you can't actually convert the TimeSpan variable into Int, but is there some sort of work around?

This is what I have so far:

DateTime startTime = DateTime.Now;
                Console.WriteLine("What is the Answer?");
                Console.WriteLine("1 - Add");
                Console.WriteLine("2 - Subtract");
                Console.WriteLine("3 - Multiply");
                Console.WriteLine("4 - Divide");
                input = Console.ReadLine();
                intInput = int.Parse(input);
                if (intInput == 1)
                    DateTime stopTime = DateTime.Now;
                    TimeSpan duration = stopTime - startTime;
                    Console.WriteLine("Correct! Well Done!");
                    Console.WriteLine("Your Result:");
                    Console.WriteLine("Incorrect answers given: " + (incorrect));
                    Console.WriteLine("Time Taken: " + duration.Milliseconds + " Milliseconds");
                    duration = array;
                    goto start;

As you can see I have two DateTime.Now functions representing the start and finish, then the TimeSpan function which subtracts the two together to give me a time which is saved in variable duration. Then i've printed the time out to the application in the form of milliseconds.

Also, my array has 10 locations to store these times in. I want to be able to store each time in a particular location according to the value. For example, if I have a value of 5000 milliseconds, I want it to be stored in array location 5.

Any help on this matter would be greatly appreciated!

Kind regards,

Re: Converting TimeSpan into int for an Array? 80 80

Seems you rather like to store the time in seconds, or am I wrong?
A TimeSpan has several properties you can look up here: http://msdn.microsoft.com/en-us/library/system.timespan_members(VS.71).aspx
You could look up how many secs it took and then store the millisecs of the timespan into that array slot.
Hope it helps.

Re: Converting TimeSpan into int for an Array? 80 80

Firstly, i would suggest you look at the TotalMillisecond property rather than the Millisecond which only stores the millisecond portion of the total. eg, if someone took 30.22 seconds then the Milliseconds would return 220 whilst the TotalMilliseconds would return 30220.

Secondly, what is your intent when you say you want to store the results that way? Where would you store a result of 5400, or 5600? If both would be stored in position 5 then which would you store if you got both results? If you let us know what you hope to achieve we can let you know if your going about it the right way :)

Re: Converting TimeSpan into int for an Array? 80 80

Thanks for the reply, I've changed to TotalMilliseconds. Thanks for that! As for the saving of times, I don't want the programme to save all the times, I'd rather it overwrite the original.

Re: Converting TimeSpan into int for an Array? 80 80

Just so i know i've understood; you want 5000 to go in array item 5, and 6000 would go in 6. So 5999 would go in 5? What happens if the time is more than 10999? Where would times of 11000 go?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.