Even Handler problem
-
Hi! I am trying to run a timer in my Form. The form has a function that is being called from the timer handler but i have a problem. this is the code: private static void TimerHandler(Object myObject, EventArgs myEventArgs) { starttime.Stop(); starttime.Enabled = false; Form1_Main(); } if i use the static on the Hnaler i get this error: Error 1 An object reference is required for the nonstatic field, method, or property 'Update_Installer.Form1.Form1_Main()' C:\Documents and Settings\Yossi_Tubis\My Documents\Visual Studio 2005\Projects\Update_Installer\Update_Installer\Update_Installer\Form1.cs 124 13 Update_Installer if i do not use it i get this: private void TimerHandler(Object myObject, EventArgs myEventArgs) { starttime.Stop(); starttime.Enabled = false; Form1_Main(); } Error: Error 1 An object reference is required for the nonstatic field, method, or property 'Update_Installer.Form1.TimerHandler(object, System.EventArgs)' C:\Documents and Settings\Yossi_Tubis\My Documents\Visual Studio 2005\Projects\Update_Installer\Update_Installer\Update_Installer\Form1.cs 112 31 Update_Installer The function is in the general Form. What to do? Thanks :)
-
Hi! I am trying to run a timer in my Form. The form has a function that is being called from the timer handler but i have a problem. this is the code: private static void TimerHandler(Object myObject, EventArgs myEventArgs) { starttime.Stop(); starttime.Enabled = false; Form1_Main(); } if i use the static on the Hnaler i get this error: Error 1 An object reference is required for the nonstatic field, method, or property 'Update_Installer.Form1.Form1_Main()' C:\Documents and Settings\Yossi_Tubis\My Documents\Visual Studio 2005\Projects\Update_Installer\Update_Installer\Update_Installer\Form1.cs 124 13 Update_Installer if i do not use it i get this: private void TimerHandler(Object myObject, EventArgs myEventArgs) { starttime.Stop(); starttime.Enabled = false; Form1_Main(); } Error: Error 1 An object reference is required for the nonstatic field, method, or property 'Update_Installer.Form1.TimerHandler(object, System.EventArgs)' C:\Documents and Settings\Yossi_Tubis\My Documents\Visual Studio 2005\Projects\Update_Installer\Update_Installer\Update_Installer\Form1.cs 112 31 Update_Installer The function is in the general Form. What to do? Thanks :)
ytubis wrote:
Form1_Main();
That 2nd error you get - what line of code is the error occurring on? My bet is you have some static timer and you're telling it to send it's Tick event notification to a non-static method. To solve this, just don't make the timer static, don't make TimerHandler static, and don't make Form1_Main static. Don't make anything static unless there's a specific reason to do so (e.g. if it doesn't need to use any instance members)
Tech, life, family, faith: Give me a visit. I'm currently blogging about: Sound The Great Shofar! The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
Hi! I am trying to run a timer in my Form. The form has a function that is being called from the timer handler but i have a problem. this is the code: private static void TimerHandler(Object myObject, EventArgs myEventArgs) { starttime.Stop(); starttime.Enabled = false; Form1_Main(); } if i use the static on the Hnaler i get this error: Error 1 An object reference is required for the nonstatic field, method, or property 'Update_Installer.Form1.Form1_Main()' C:\Documents and Settings\Yossi_Tubis\My Documents\Visual Studio 2005\Projects\Update_Installer\Update_Installer\Update_Installer\Form1.cs 124 13 Update_Installer if i do not use it i get this: private void TimerHandler(Object myObject, EventArgs myEventArgs) { starttime.Stop(); starttime.Enabled = false; Form1_Main(); } Error: Error 1 An object reference is required for the nonstatic field, method, or property 'Update_Installer.Form1.TimerHandler(object, System.EventArgs)' C:\Documents and Settings\Yossi_Tubis\My Documents\Visual Studio 2005\Projects\Update_Installer\Update_Installer\Update_Installer\Form1.cs 112 31 Update_Installer The function is in the general Form. What to do? Thanks :)
Hi, I am guessing you are working on an app without a user interface, and are trying to run everything from your static Main method and feel a need to have everything static then. The easy solution to that is to immediately create an object of a new class and make it run; then nothing needs to be static except for the main method. Example:
static Main(...) {
Test test=new Test();
test.Run();
}class Test {
Timer timer;public Test() { timer=new Timer(); timer.Tick+=new EventArgs(tickHandler); } public void Run() { ... } public void tickHandler(...) { ... }
}
BTW: you can put your static Main method inside the Test class if you want to. It does not need a separate file or class. :)
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google