I have the following struct
private struct sData{
public int volume;
public System.Timers.Timer aliveTimer;
public void OnTimedEvent(object source, ElapsedEventArgs e)
{
Console.WriteLine("this = " + volume);// I cannot access volume here
}
}
and when the time elapsed i want to change volume value and i can’t access it in OnTimedEvent
I use this code to start the struct and the timer:
sData ret = new sData();
ret.volume = rand.Next(1, 10) * 100;
ret.aliveTimer = new System.Timers.Timer(1000);
ret.aliveTimer.Elapsed += new ElapsedEventHandler(sData.OnTimedEvent);
ret.aliveTimer.Start();
what should I do?
I would rewrite it to:
Using a class is always a better idea than a struct. Furthurmore Data contains a timer and yet you are setting the OnTimedEvent handler (which is defined in Data) from another component. Bit unlogical.