-1

I have this code:

System.Timers.Timer ti1, ti2, ti3;

private void button1_Click(object sender, EventArgs e)
{
   if(_1 == true)
{
    ti1 = new System.Timers.Timer(1000);
    ti1.Interval = 1000;
    ti1.Elapsed += new System.Timers.ElapsedEventHandler(tiel1);
    ti1.Enabled = true;
}
else
{
    ti1.Stop();
}
}

It successfully works, but because I do not want to use the same code, I created a helper method:

private void my_timer(System.Timers.Timer tim, System.Timers.ElapsedEventHandler Tick)
        {
            tim = new System.Timers.Timer(1000);
            tim.Interval = 1000;
            tim.Elapsed += new System.Timers.ElapsedEventHandler(Tick);
            tim.Enabled = true;
        }

and then I call it my_timer(ti1, tiel1)

but when ti1.Stop() is called it gives me this error "object reference not set to an instance of an object".

Any idea why? Thanks.

coldfire
  • 55
  • 1
  • 7
  • It looks like they should be, but can you verify that the created timer(s) in the helper are actually running? – SteveFerg Jul 03 '15 at 18:24
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Dmitry Jul 03 '15 at 19:08
  • @Dmitry it is not a duplicate so please remove the downvote if you downvoted. :/ – coldfire Jul 04 '15 at 16:04

2 Answers2

1

In C#, parameters are generally passed by value.

Here's what it means.

When you assign a new value to a method parameter, like this:

tim = new System.Timers.Timer(1000);

it means that from now on tim will be a reference to a new Timer, but it does not affect the reference you passed from outside (ti1). So in your case ti1 is still a null reference, because you never assigned any object to it.

ti1 and tim are not the same reference. If you assign (=) something to one, it does not affect the other.

Possible solution

Instead of declaring tim as an argument, you should return the new object from the method:

var tim = ...

...

return tim

and assign the result of the method to ti1:

ti1 = my_timer( ... )

Ilya Kogan
  • 21,995
  • 15
  • 85
  • 141
1

It is because in c# method parameters are passed by value not by reference.

In case of your code System.Timers.Timer ti1, ti2, ti3; all 3 are reference of System.Timers.Timer containing null values.

When you have passed ti1 as parameter, it passed the value null of ti1 so, after executing the following expression : tim = new System.Timers.Timer(1000); it has the reference of System.Timers.Timer while ti1 is containing null value still

So, Rearrange your my_timer method as following to achieve your expected behavior

private System.Timers.Timer my_timer(System.Timers.Timer tim, System.Timers.ElapsedEventHandler Tick)
    {
        tim.Interval = 1000;
        tim.Elapsed += new System.Timers.ElapsedEventHandler(Tick);
        tim.Enabled = true;
        return tim;
    }

and then trigger the method and use the returned value as following

System.Timers.Timer ti1, ti2, ti3;

private void button1_Click(object sender, EventArgs e)
{
   ti1 = new System.Timers.Timer(1000);
   if(_1 == true)
   {
      ti1 = my_timer(ti1, tiel1)
   }
   else
   {
     ti1.Stop();
   }
}
PaulShovan
  • 2,140
  • 1
  • 13
  • 22