Skip to main content
gamedev.net
🔒 Locked 🎮 Unity

[.net] Timer objects issues with C#/.NET 3.0

Started by Nali 0 replies 1.8k views
Original Post
Nali
Nali
Hello all,
I´m having issues with getting Timer objects to work correctly in my game (Memory Game).

Goal: Make 2 timers, one with 1s delay for calculating the time it takes for a player to finish a round and one timer with ~100 ms maybee for a visual effect when matching memory blocks.

I encapsulated a Timer object into a user-defined class (CTimer), very basic one but I had to make the Timer object within the class public because I dont know how to keep it private and still make it to work with the code below.

The CTimer class:

using System.Timers;

class CTimer
{
public Timer objTimer; // This should be private

public CTimer(int msInt)
{
objTimer = new Timer(msInt);
objTimer.AutoReset = true;
}

public void SetIntervalMS(int msInt)
{
objTimer.Interval = msInt;

}

public void StartClock()
{
objTimer.Start();
}

public void StopClock()
{
objTimer.Stop();

}

~CTimer()
{
this.StopClock();
objTimer.Dispose();
}


And this is in Form.cs:


public partial class frmMain : Form
{
private CTimer gameRndTimer;
private Memory theGame; // the main game code class.


void NewGame(){
// doing a lot of initializing..

gameRndTimer = new CTimer(1000);
gameRndTimer.objTimer.Elapsed += new ElapsedEventHandler(OnTimerUpdate);
gameRndTimer.StartClock();

}

// using "private static void OnTimerUpdate(...) generates this error:
// An object reference is required for the non-static field, method, or property 'WinMemory.frmMain.theGame' H:\Project\WinMemory\WinMemory\Form.cs 117 69 WinMemory

void OnTimerUpdate(object source, ElapsedEventArgs e)
{
// updating visuals etc
....
}

public void InvokeGameOver(....)
{
....

if (gameoverDialog == DialogResult.Yes)
{
....
gameRndTimer.StopClock();
//gameRndTimer = null;
}
}

}



1. How can I make the Timer object in CTimer private and still be able to have Timer.Elapsed accessable for the EventHandler?

2. How come I cannot use "private static void OnTimerUpdate() ?

Edit: updated, had issue with timer not working, got it to work now .. but the 2 questions above are issues I want help with.
Thanks in advance. All help appreciated.
Drew_Benton
Drew_Benton

1. How can I make the Timer object in CTimer private and still be able to have Timer.Elapsed accessable for the EventHandler?


One way could be to add functions that take a ElapsedEventHandler parameter and then perform the add/remove operation on the private object.

public void AddElapsed(System.Timers.ElapsedEventHandler handler)
{
objTimer.Elapsed += handler;
}

public void RemoveElapsed(System.Timers.ElapsedEventHandler handler)
{
objTimer.Elapsed -= handler;
}


If you wanted a cleaner way to handle a private member variable publicly exposed, you could just use properties:


public System.Timers.Timer Timer { get { return objTimer; } }


I'd go with the properties approach since that's why C# has them!

Then, you can just call: gameRndTimer.Timer.Elapsed += new ElapsedEventHandler(OnTimerUpdate);

2. How come I cannot use "private static void OnTimerUpdate() ?[/quote]

The compiler already told you why!

An object reference is required for the non-static field, method, or property 'WinMemory.frmMain.theGame'[/quote]

Since you have a static function, you must only access static variables. "private Memory theGame;" is not a static variable, so you cannot access it.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.