错误:扩展方法必须在顶级静态类中定义(CS1109) [英] Error: Extension methods must be defined in a top level static class (CS1109)
本文介绍了错误:扩展方法必须在顶级静态类中定义(CS1109)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试制作一个倒计时程序,如果需要的话,我可以启动和停止该程序并将倒计时的值设置为10分钟。
I'm trying to make a countdown program, which I can start and stop and set the value of the countdown to 10 minutes if needed.
但是我我收到一个错误消息,我不太了解。
我不喜欢C#,所以代码如下:
But I'm getting an error I don't quite understand. I'm not that into C#, so here's the code:
有人可以在这里帮我吗?认为我在3.0框架上运行吗?
Can some one help me a bit here ? Think I run on framework 3.0 or something ?
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();
}
}
}
}
推荐答案
错误消息确切说明了问题所在:您的 IntExt
方法不是顶级静态类。这是一个嵌套静态类。只需将其从 MainForm
中拉出,就可以了。
The error message says exactly what's wrong: your IntExt
method isn't a top-level static class. It's a nested static class. Just pull it out of MainForm
and it'll be fine.
这篇关于错误:扩展方法必须在顶级静态类中定义(CS1109)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文