6

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.

But I'm getting an error I don't quite understand. I'm not that into C#, so here's the code:

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();
            }
        }     
    }       
}
RickL
  • 3,318
  • 10
  • 38
  • 39
Patrick R
  • 1,949
  • 3
  • 32
  • 58

2 Answers2

15

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thanks, but now im getting this one: The namespace 'PauseMaster' already contains a definition for 'IntExt' (CS0101) – Patrick R Oct 25 '12 at 14:56
  • 1
    @PatrickR: So, that suggests you've already got such a class (which should be called `Int32Ext` or `Int32Extensions`, btw). Assuming that's already a static non-generic class, just move `AddZero` into there. – Jon Skeet Oct 25 '12 at 15:10
  • Sorry if im an idiot here, but cant seem to find any other class's called Int32Ext or Int32Extensions. Altho i had another .cs file where the IntExt was again, deleted it and got 9 new error's .. totally lost here :: new errors: 'System.Windows.Forms.TextBox' does not contain a definition for 'TextBox' and no extension method 'TextBox' accepting a first argument of type 'System.Windows.Forms.TextBox' could be found – Patrick R Oct 25 '12 at 15:31
  • @PatrickR: Why did you delete the existing `IntExt`? I only said it should be called `Int32Ext` in order to follow .NET naming conventions. You should add the `AddZero` method to the existing class, *then* rename it. I've no idea where the `TextBox` errors came from though, and without seeing the code it's really impossible to guess... – Jon Skeet Oct 25 '12 at 16:04
-4

I know this question is old, but in case anyone else runs into this problem...

Check to make sure that the keyword "this" is not in the parameter name definition (usually from copy/paste or drag)

So, replace:

public static string AddZero(this int i)

with this:

public static string AddZero(int i)

That should solve your problem.

dougczar
  • 585
  • 7
  • 8
  • 5
    Yeah, this solves the problem by no longer having the method act like an extension method, so it cannot be applied to types! Not a solution if you actually want to use extension methods! – dalemac Oct 18 '17 at 08:51