English 中文(简体)
我为什么不增加时间?
原标题:Why is my Stopwatch not adding time?

班级在“快车”的“停止监视”时间上取得了一定进步。 AddTotime(h,m, s)

页: 1

class Speedrun
    {
        private static Stopwatch sr = new Stopwatch();

        public static void Go()
        {
            sr.Reset();
            sr.Start();
        }

        public static string GetTime
        {
            get
            {
                return sr.Elapsed.Hours + "h " + sr.Elapsed.Minutes + "m " + sr.Elapsed.Seconds + "s";
            }

        }

        public static void AddToTime(int hours, int minutes, int seconds)
        {
            TimeSpan ts = new TimeSpan(hours, minutes, seconds);
            sr.Elapsed.Add(ts);

        }

        public static void Stop()
        {
            sr.Stop();
        }

        public static void Cont()
        {
            sr.Start();
        }
    }

公平直截了当,我指的是使用阶级名称,而不是一个变数。 这里的情况

Speedrun.Go();

任何帮助都将受到高度赞赏。

www.un.org/spanish/ecosoc UPDATE!

这里发现的“CHEAP”解决办法是帮助那些可能面临类似情况的gu。

class Speedrun
{
    private static Stopwatch sr = new Stopwatch();

    private static int addedhours = 0;
    private static int addedminutes = 0;
    private static int addedseconds = 0;


    public static void Go()
    {
        sr.Reset();
        sr.Start();
        addedhours = 0;
        addedminutes = 0;
        addedseconds = 0;
    }

    public static string GetTime
    {
        get
        {
            return (sr.Elapsed.Hours + addedhours) + "h " + (sr.Elapsed.Minutes + addedminutes) + "m " + (sr.Elapsed.Seconds + addedseconds) + "s";
        }

    }

    public static void AddToTime(int hours, int minutes, int seconds)
    {
        addedhours = addedhours + hours;
        addedminutes = addedminutes + minutes;
        addedseconds = addedseconds + seconds;

    }

    public static void Stop()
    {
        sr.Stop();
    }

    public static void Cont()
    {
        sr.Start();
    }
}
问题回答

页: 1 _immutable 价值类型。 您在此无所作为:

sr.Elapsed.Add(ts);

......这样,它实际上就失去了选择。 即便是<条码>日码>(日码>/代码>)是不可变的,它仍是一个价值类型,因此,你要打上财产价值(复制件),然后打上<条码>Add<>>/代码/代码>,然后将复制件丢失。

基本上,你试图在这里做些什么都赢得了工作。

EDIT:你的工作方式没有真正奏效,因为如果需要35秒钟,你再增加30秒,你就要报告65秒而不是1分钟和5秒。

您应当保留一个<条码>日码<>。

public static string GetTime
{
    get
    {
        TimeSpan time = sr.Elapsed + timeToAdd;
        // In .NET 4 you could use a custom format string for the TimeSpan
        return string.Format("{0}h {1}m {2}s",
                             time.Hours, time.Minutes, time.Seconds);
    }
}

我对总体设计有点怀疑,特别是,所有这些静态,都不会试图表面上安全,使我变得虚弱。 页: 1 所有这一切都是静态的?

时间Span.Add()确实没有改变现有价值,但又回到了新的时间Span。 因此,你们需要改变:

sr.Elapsed.Add(ts);

......这样,它实际上会把结果放在某种有益的位置上。

<代码>StopWatch.Elapsed

页: 1 StopWatch 反对。

引证:

class Speedrun
{
    private static Stopwatch sr = new Stopwatch();
    private static TimeSpan elapsed = new TimeSpan();

    public static void Go()
    {
        elapsed = new TimeSpan(0);
        sr.Reset();
        sr.Start();
    }

    public static string GetTime
    {
        get
        {
            elapsed = elapsed.Add(sr.Elapsed);
            return elapsed.Hours + "h " + elapsed.Minutes + "m " + elapsed.Seconds + "s";
        }

    }

    public static void AddToTime(int hours, int minutes, int seconds)
    {
        TimeSpan ts = new TimeSpan(hours, minutes, seconds);
        elapsed = elapsed.Add(ts);
    }

    public static void Stop()
    {
        sr.Stop();
    }

    public static void Cont()
    {
        sr.Start();
    }
}

FYI,Gettime is an appropriate name for a method. property>的更名将简单地改为time Elapsedtime

这是我的解决办法。

public class StopwatchPlus : Stopwatch
{
    private TimeSpan AddedTime { get; set; }
    public StopwatchPlus() { AddedTime = new TimeSpan(0); }
    public TimeSpan Elapsed { get { return base.Elapsed.Add(AddedTime); } }
    public void Add(TimeSpan time) { AddedTime = AddedTime.Add(time); }
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签