Reputation: 1031
I have the following code, I want to call data1()
from data2()
. Is this possible in C#? If so, how?
private void data1()
{
}
private static void data2()
{
data1(); //generates error
}
Upvotes: 95
Views: 272260
Reputation: 1
Please I think the response to your question could be :
public class <Classname> {
static method() {
(new <Classname>)->non-static();
}
non-static method(){ ...; }
~<Classname>(){...;}
};
When a data member is declared as static, only one copy of the data is maintained for all objects of the class. A static member has or handles a permanent storage.
for C++ and probably C#, when new operator is used, it allocates memory for a class object, the object constructor is called after the memory is allocated. Thus, as a static member is called preceded by it belonging classname, the same way the non-static member is called. Thus, the use of (new Classname) seems like a call of a member with predefined permanent storage like a static data member.
I hope these details will help you.
Upvotes: 0
Reputation: 11
new Foo();
Foo.StaticMethod();
class Foo
{
private static Foo foo;
public Foo()
{
foo = this;
}
private void PrintHello()
{
Console.WriteLine("Hello");
}
public static void StaticMethod()
{
foo.PrintHello();
}
}
Upvotes: 1
Reputation: 21
You can use call method by like this : Foo.Data2()
public class Foo
{
private static Foo _Instance;
private Foo()
{
}
public static Foo GetInstance()
{
if (_Instance == null)
_Instance = new Foo();
return _Instance;
}
protected void Data1()
{
}
public static void Data2()
{
GetInstance().Data1();
}
}
Upvotes: 1
Reputation: 92
Static method never allows a non-static method call directly.
Reason: Static method belongs to its class only, and to nay object or any instance.
So, whenever you try to access any non-static method from static method inside the same class: you will receive:
"An object reference is required for the non-static field, method or property".
Solution: Just declare a reference like:
public class <classname>
{
static method()
{
new <classname>.non-static();
}
non-static method()
{
}
}
Upvotes: -1
Reputation: 1255
Assuming that both data1()
and data2()
are in the same class, then another alternative is to make data1()
static.
private static void data1()
{
}
private static void data2()
{
data1();
}
Upvotes: -2
Reputation: 16272
Apologized to post answer for very old thread but i believe my answer may help other.
With the help of delegate the same thing can be achieved.
public class MyClass
{
private static Action NonStaticDelegate;
public void NonStaticMethod()
{
Console.WriteLine("Non-Static!");
}
public static void CaptureDelegate()
{
MyClass temp = new MyClass();
MyClass.NonStaticDelegate = new Action(temp.NonStaticMethod);
}
public static void RunNonStaticMethod()
{
if (MyClass.NonStaticDelegate != null)
{
// This will run the non-static method.
// Note that you still needed to create an instance beforehand
MyClass.NonStaticDelegate();
}
}
}
Upvotes: 7
Reputation: 399
You just need to provide object reference . Please provide your class name in the position.
private static void data2()
{
<classname> c1 = new <classname>();
c1. data1();
}
Upvotes: 11
Reputation: 3813
Perhaps what you are looking for is the Singleton pattern?
public class Singleton
{
private Singleton() {}
public void DoWork()
{
// do something
}
// You can call this static method which calls the singleton instance method.
public static void DoSomeWork()
{
Instance.DoWork();
}
public static Singleton Instance
{
get { return instance; }
}
private static Singleton instance = new Singleton();
}
You still have to create an instance of the class but you ensure there is only one instance.
Upvotes: 29
Reputation: 4970
You have to create an instance of that class within the static method and then call it.
For example like this:
public class MyClass
{
private void data1()
{
}
private static void data2()
{
MyClass c = new MyClass();
c.data1();
}
}
Upvotes: 25
Reputation: 14196
You can't call a non-static method without first creating an instance of its parent class.
So from the static method, you would have to instantiate a new object...
Vehicle myCar = new Vehicle();
... and then call the non-static method.
myCar.Drive();
Upvotes: 12
Reputation: 532445
You'll need to create an instance of the class and invoke the method on it.
public class Foo
{
public void Data1()
{
}
public static void Data2()
{
Foo foo = new Foo();
foo.Data1();
}
}
Upvotes: 146