Erroneous staticity



  • When used:

    public static void OnTimedEvent(Object source, ElapsedEventArgs e)
    {
        webBrowser1.Refresh();
    }
    

    Issues:

    CS0120 error for non-static field, method or properties Form1.webBrowser1 requires a reference to the object. Form1.cs 101

    If not webBrowser1 set. Form1.webBrowser1it doesn't work either.

    P.S webBrowser1 has a retrofit. public

    If you take it away. staticthe error is cleaned up, but it's no longer a function with a similar error.



  • The problem can be solved as follows.

    Introduce the first parameter of the method to the type of shape and use the non-static field through it. For example:

    public static void MyStaticMethod(Object source)
    {
        ((Form1)source).webBrowser1.Refresh();
    }
    

    Naturally, this method should be transmitted in one way or another existing The object of the form.


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2