Error: Extension methods must be defined in a static top-level class (CS1109)

im trying to make a countdown program with which i can start and stop, and if necessary set the countdown to 10 minutes.

But I get an error, I do not quite understand. Im not that in C #, so here is the code:

Can someone help me a little? I think I'm running framework 3.0 or something like that?

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
source share
2 answers

The error message states exactly what is wrong: your IntExt method IntExt not a static top-level class. This is a nested static class. Just pull it out of MainForm and everything will be fine.

+13
source

I know this question is old, but in case someone else comes across this problem ...

Make sure the keyword "this" is not specified in the parameter name definition (usually from copy / paste or drag and drop)

So replace:

 public static string AddZero(this int i) 

with this:

 public static string AddZero(int i) 

This should solve your problem.

-1
source

Source: https://habr.com/ru/post/1259301/


All Articles