Look at this code. When I run it, pushing the button should create a new object, and the old should be deleted, but thats not what happening. Compile the code and try it.
How can this be?
/J Lindroos
using System.Timers;
using System;
using System.Windows.Forms;
using System.Drawing;
public interface ITimerListener {
void Tick(int seconds);
}
public class TimerClass {
System.Timers.Timer t=null;
int seconds;
ITimerListener listener;
public TimerClass(ITimerListener listener) {
seconds=0;
this.listener=listener;
t=new System.Timers.Timer();
t.Elapsed+=new ElapsedEventHandler(Tick);
t.Interval=1000;
t.Enabled=true;
Console.WriteLine("New timerclass");
}
public void Tick(object source, ElapsedEventArgs e) {
seconds++;
listener.Tick(seconds);
}
}
public class TestClass : ITimerListener {
TimerClass tc;
public TestClass() {
//Making the form and the button
Form f=new Form();
f.Size=new Size(100,100);
Button b=new Button();
b.Text="Start timer";
b.Size=new Size(90,32);
b.Location=new Point(5,32);
b.Click+=new EventHandler(b_Click);
f.Controls.Add(b);
f.ShowDialog();
}
public void b_Click(object sender, EventArgs e) {
//Creating a NEW TimerClass
tc=new TimerClass(this);
}
public void Tick(int seconds) {
//Writing the seconds
Console.WriteLine(seconds);
}
public static void Main(string[] args) {
TestClass t=new TestClass();
}
}