6

カウントダウンプログラムを作成しようとしています。このプログラムを開始および停止し、必要に応じてカウントダウンの値を10分に設定できます。

しかし、よくわからないエラーが発生します。私はC#に興味がないので、コードは次のとおりです。

誰かがここで私を少し助けてくれますか?フレームワーク3.0か何かで実行していると思いますか?

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.Timers;

namespace PauseMaster
{   
    public partial class MainForm : Form
    {           
        public MainForm()
        {           
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {               
        }

        private DateTime endTime;
        private void btnStartStop_Click(object sender, EventArgs e)
        {
            if(btnStartStop.Text == "START")
            {
                int hours = int.Parse(string.IsNullOrEmpty(txtCountFromHour.TextBox.Text) || txtCountFromHour.TextBox.Text == "timer" ? "0" : txtCountFromHour.TextBox.Text);
                int mins = int.Parse(string.IsNullOrEmpty(txtCountFromMin.TextBox.Text) || txtCountFromMin.TextBox.Text == "minutter" ? "0" : txtCountFromMin.TextBox.Text);
                int secs = int.Parse(string.IsNullOrEmpty(txtCountFromSec.TextBox.Text) || txtCountFromSec.TextBox.Text == "sekunder" ? "0" : txtCountFromSec.TextBox.Text);

                endTime = DateTime.Now.AddHours(hours).AddMinutes(mins).AddSeconds(secs);

                timer1.Enabled = true;  
                btnStartStop.Text = "STOP";
            }
            else
            {
                timer1.Enabled = false;
                btnStartStop.Text = "START";
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (endTime <= DateTime.Now)
            {
                timer1.Enabled = false;
                lblTimer.Text = "00:00:00";
                btnStartStop.Text = "Start";
                return;
            }

            TimeSpan ts = endTime - DateTime.Now;
            lblTimer.Text = string.Format("{0}:{1}:{2}.{3}", ts.Hours.AddZero(), ts.Minutes.AddZero(), ts.Seconds.AddZero());
        }

        public static class IntExt
        {
            public static string AddZero(this int i) // GETTING ERROR HERE AT 'AddZero'
            {
                int totLength = 2;
                int limit = (int)Math.Pow(10, totLength - 1);
                string zeroes = "";
                for (int j = 0; j < totLength - i.ToString().Length; j++)
                {
                    zeroes += "0";
                }
                return i < limit ? zeroes + i : i.ToString();
            }
        }     
    }       
}
4

2 に答える 2

15

エラーメッセージは、何が問題なのかを正確に示しています。IntExtメソッドトップレベルの静的クラスではありません。これはネストされた静的クラスです。引き抜くだけMainFormで大丈夫です。

于 2012-10-25T14:53:31.860 に答える
-3

私はこの質問が古いことを知っていますが、誰かがこの問題に遭遇した場合に備えて...

キーワード「this」がパラメータ名の定義に含まれていないことを確認してください(通常はコピー/貼り付けまたはドラッグから)

したがって、次を置き換えます。

public static string AddZero(this int i)

これとともに:

public static string AddZero(int i)

それはあなたの問題を解決するはずです。

于 2017-09-15T18:05:26.097 に答える