-1

我有这个代码,它将是一个秒表。

为什么不能通过实例引用访问?

错误:无法使用实例引用访问成员“System.Diagnostics.Stopwatch.GetTimestamp()”;而是用类型名称来限定它。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;

namespace Pocitadlo
{
public partial class Form1 : Form
{
    public int minutys = 0, test = 0;
    Thread sekund;
    TimeSpan ts;
    Stopwatch stopky = new Stopwatch();

    public Form1()
    {
        InitializeComponent();
    }

    public void START_Click(object sender, EventArgs e)
    {
            sekund = new Thread(Sekund_pocet);
            this.START.Visible = false;
            this.STOP.Visible = true;
            sekund.Start();
    }

    public void Sekund_pocet()
    {
        stopky.Start();
        stopky.Stop();
        ts = stopky.GetTimestamp();
        string time = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
        cas.BeginInvoke(new Action(() => cas.Text = Convert.ToString(time)));
    }

    private void STOP_Click(object sender, EventArgs e)
    {
        this.STOP.Visible = false;
        this.START.Visible = true;
        test = 1;
    }
}
}
4

1 回答 1

5

GetTimestamp()是一个静态方法,因此是错误的。你应该这样做ts = Stopwatch.GetTimestamp()

于 2013-04-21T13:42:05.303 回答