Click here to Skip to main content
15,920,217 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how can i return totaltime as a timespan type's?
or my solutin is right here?
and if you think this code have a mistake tell me ?

look at this code please and try to solve my problem .
activity is a list and timelenght equals endtime - starttime
i want to adding all of timeleght's here as totaltime and i dont know how can i return totaltime?

plaease help me?

but anywhere thank you

public TimeSpan calculateTotaltime()

    {
        foreach (Activity a in myData.activities)
        {
                a.totaltime += a.timelenght;


        }
        return   ?       ;

   }

help please
Posted
Updated 1-May-11 20:56pm
v7

DateTime then = DateTime.Now;
Thread.Sleep(1000);
TimeSpan span = DateTime.Now - then;
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 2-May-11 2:10am    
No, this is not what OP is having a problem with...
Please see my answer.
--SA
shiiiiir 2-May-11 2:17am    
i think we have a missunderstanding here ?
activity is a list and timelenght equals endtime - starttime

i want to adding all of timeleght's here as totaltime and i dont know how can i return totaltime?
but anywhere thank you
I don't know exactly what Activity type do you mean, so I don't know the type of the timelength. Depending on the units of you can create an instance of System.TimeSpan. For example, use static method System.TimeSpan.FromMilliseconds.

There are also the methods FromTicks, FromSeconds, FromMinutes, etc.
See http://msdn.microsoft.com/en-us/library/system.timespan.aspx[^].

—SA
 
Share this answer
 
v2
Comments
shiiiiir 2-May-11 2:17am    
i think we have a missunderstanding here ?
activity is a list and timelenght equals endtime - starttime

i want to adding all of timeleght's here as totaltime and i dont know how can i return totaltime?
but anywhere thank you
Sergey Alexandrovich Kryukov 2-May-11 3:41am    
It does not matter what activity is. I answered the way it will work anyway. If Activity.timelength (honestly, observe proper capitalization by meeting (good) Microsoft naming conventions), it it is of the type TimeSpan, just return total time. As you write "?" at return, it means it's of some other type. Name it. I assume this is some integer representing milliseconds, seconds, etc. Then use System.TimeSpan.FromMilliseconds to get TimeSpan to return.
--SA
If I understood your question correctly, what you need to do is sum all of activities' durations to get the total like this:
public TimeSpan calculateTotaltime()
{
    TimeSpan total = TimeSpan.Zero;
    foreach (Activity a in myData.activities)
        total += a.timelength;

    return total;
}


Here total is the sum of the durations of all the activities in your list.
 
Share this answer
 
v2
Comments
shiiiiir 2-May-11 3:45am    
that's it very thankyou.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900