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.
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()
{
}
}
You need to have a instance of the class to call a non-static method.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With