Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Even Handler problem

Even Handler problem

Scheduled Pinned Locked Moved C#
helpcsharpvisual-studioquestionannouncement
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    ytubis
    wrote on last edited by
    #1

    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 :)

    J L 2 Replies Last reply
    0
    • Y ytubis

      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 :)

      J Offline
      J Offline
      Judah Gabriel Himango
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • Y ytubis

        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 :)

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        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


        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups