gpt4 book ai didi

c# - 错误 : Extension methods must be defined in a top level static class (CS1109)

转载 作者:太空狗 更新时间:2023-10-29 22:23:22 24 4
gpt4 key购买 nike

我正在尝试制作一个倒计时程序,如果需要,我可以启动和停止并将倒计时的值设置为 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();
}
}
}
}

最佳答案

错误消息准确说明了问题所在:您的 IntExt 方法不是顶级静态类。这是一个嵌套 静态类。只需将其从 MainForm 中拉出即可。

关于c# - 错误 : Extension methods must be defined in a top level static class (CS1109),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13071296/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com