C# 错误:必须在顶级静态类(CS1109)中定义扩展方法
我正在尝试制作一个倒计时程序,如果需要,我可以启动和停止该程序,并将倒计时值设置为10分钟 但我犯了个错误,我不太明白。 我不太喜欢C#,所以代码如下: 有人能帮我一点忙吗?你觉得我运行的是框架3.0还是什么C# 错误:必须在顶级静态类(CS1109)中定义扩展方法,c#,.net,extension-methods,static-classes,C#,.net,Extension Methods,Static Classes,我正在尝试制作一个倒计时程序,如果需要,我可以启动和停止该程序,并将倒计时值设置为10分钟 但我犯了个错误,我不太明白。 我不太喜欢C#,所以代码如下: 有人能帮我一点忙吗?你觉得我运行的是框架3.0还是什么 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using Sy
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();
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用系统计时器;
命名空间PauseMaster
{
公共部分类主窗体:窗体
{
公共表格(
{
初始化组件();
}
私有void主窗体加载(对象发送方、事件参数e)
{
}
私有日期时间结束时间;
私有void btnStartStop_单击(对象发送方,事件参数e)
{
如果(btnStartStop.Text==“开始”)
{
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==“分钟”?“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(小时)。AddMinutes(分钟)。AddSeconds(秒);
timer1.Enabled=true;
btnStartStop.Text=“停止”;
}
其他的
{
timer1.Enabled=false;
btnStartStop.Text=“开始”;
}
}
私有无效计时器1_刻度(对象发送方,事件参数e)
{
如果(endTime错误消息准确地指出了错误:您的IntExt
方法不是顶级静态类。它是嵌套的静态类。只要将它从MainForm
中拉出就可以了。我知道这个问题很老,但万一其他人遇到这个问题
检查以确保关键字“this”不在参数名称定义中(通常来自复制/粘贴或拖动)
因此,替换:
public static string AddZero(this int i)
为此:
public static string AddZero(int i)
这应该可以解决你的问题。谢谢,但现在我得到了这个:名称空间“PauseMaster”已经包含了“IntExt”(CS0101)的定义@PatrickR:所以,这表明你已经有了这样一个类(应该称为Int32Ext
或Int32Extensions
,顺便说一句)。假设这已经是一个静态的非泛型类,只需将AddZero
移动到其中。如果我在这里是个白痴,很抱歉,但似乎找不到任何其他名为Int32Ext或Int32Extensions的类。虽然我在IntExt所在的位置有另一个.cs文件,但删除了它并得到了9个新错误。。这里完全丢失::新错误:'System.Windows.Forms.TextBox“”不包含“TextBox”的定义,并且没有接受类型为“System.Windows.Forms.TextBox”的第一个参数的扩展方法“TextBox”found@PatrickR:您为什么要删除现有的IntExt
?我只是说,为了遵循.NET命名约定,应该将其命名为Int32Ext
。您应该添加de>AddZero
方法到现有类,然后重命名它。我不知道文本框
错误是从哪里来的,如果没有看到代码,就真的无法猜测…是的,这解决了问题,因为该方法不再像扩展方法那样工作,因此无法应用于类型!如果您实际需要,这不是一个解决方案我想使用扩展方法!