7

Here's what I have.

public static void Person_home_phone_TextChanged(object sender, EventArgs e) { ... }

Is there any way to access non-static methods from the same or another class from inside this static method?

I need grab the text in the Person_home_phone text box and save it to a class data member.

Glimpse
  • 462
  • 2
  • 8
  • 25
  • 5
    Of course you can call non-static methods from inside a static method. Why would you think you couldn't? *Main* is a static method, so obviously you have to be able to call non-static methods from a static method. – Eric Lippert Apr 09 '13 at 14:53
  • 1
    Possible duplicate: http://stackoverflow.com/questions/1360183/how-do-i-call-a-non-static-method-from-a-static-method-in-c – Geoff Apr 09 '13 at 14:55
  • 4
    Just replace your ellipsis `...` with `sender.ToString();`. Since `ToString` is a non-static method, you have succeeded in "accessing" one! – Jeppe Stig Nielsen Apr 09 '13 at 14:58
  • 1
    your static method looks like an event handler on a textbox, probably doesn't need to be static in the first place – Sten Petrov Apr 09 '13 at 15:00
  • 1
    @JeppeStigNielsen So the sender contains TextBox.Text? Thanks. This is what I'm looking for. – Glimpse Apr 09 '13 at 15:02
  • 1
    Yes, sender _might_ be a `TextBox`. You can try `var textBox = sender as TextBox; if (textBox != null) { /* use textBox here */ }`. – Jeppe Stig Nielsen Apr 09 '13 at 15:12
  • @Glimpse - Its unlikely `sender` is a `TextBox` but you can access a local reference to any `TextBox` control you want that exists on the form within a static method. – Security Hound Apr 09 '13 at 15:15

5 Answers5

28

Example() -> Example

You would just need to create an instance of the type then call the non-static, from a static method.

public class Example(){

    public static void StaticExample()
    {

        Example example = new Example();
        example.NonStatic();
    }

    public void NonStatic()
    {

    }

}
Franckentien
  • 324
  • 6
  • 21
Gabe
  • 49,577
  • 28
  • 142
  • 181
  • what if the class is from type System.Web.UI.Page? or System.Web.UI.UserControl? can we get a instance of it? – Sara N Dec 02 '15 at 04:30
  • 3
    Suppose that the called method is owned by a form class, it means that I must instantiate a new instance of the form every time I call that method? – willy wonka Aug 10 '17 at 17:06
9

You need to have a instance of the class to call a non-static method.

Mattias Jakobsson
  • 8,207
  • 2
  • 34
  • 41
2

Solution #1: Instantiate a new instance of Car every time the method is called.

 public static void DoSomething()
        {
            Car c = new Car();
            c.NonStaticMethod();
        }

Solution #2: Pass a Car to the method.

 public static void DoSomething(Car c)
        {
            c.NonStaticMethod();
        }

Solution #3:

Use a singleton Car to support the static method. (If calls from multiple threads are a possibility, you might also need locking. Note that System.Windows.Forms.Timer does not introduce a thread.)

 public class Car
    {
        private static Car m_Singleton = new Car();

        public static void DoSomething()
        {
            m_Singleton.NonStaticMethod();
        } 

Note that you have not explained your memory problems with Timer. It is very possible that there is a solution to that underlying problem.

Kiran.Bakwad
  • 614
  • 7
  • 4
1

Instance methods (vel. non-static) can only be called in context of an instance of that class. So you can call it, but you have to have an object of that class available somewhere in your static method.

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
1

A non-static method requires an instance of the class. Unless you have passed in an instance, or created an instance in your method, you cannot call a non-static method, as you have no idea what instance of the class that method should operate on.

You need an instance of the class class to call the non-static method.

Leo Chapiro
  • 13,678
  • 8
  • 61
  • 92