-3

I have the following code, I want to call data1() from data2()

 private void data1()
   {
   }
   private static void data2()
   {
       data1(); //generates error
   }
Joe
  • 29,416
  • 12
  • 68
  • 88
nightxx
  • 25
  • 6
  • 1
    by creating object.... – Jatin Khurana Oct 04 '14 at 14:39
  • 1
    I think before doing stuff in java or any ooops language. you should go through with OOPS concept... It's a very basic concept. – Jatin Khurana Oct 04 '14 at 14:41
  • its C# and i only need one think why no one helps me ? – nightxx Oct 04 '14 at 14:43
  • 1
    Because this forum is not for learning.... first learn and if you face difficulty then post here.. noone is going to answer such a basic question of oops – Jatin Khurana Oct 04 '14 at 14:44
  • And to access `data2()` use `ClassName.methodName()` syntax. – Tirath Oct 04 '14 at 14:46
  • 3
    Where is this example from? This question is *identical* to http://stackoverflow.com/questions/1360183/how-do-i-call-a-non-static-method-from-a-static-method-in-c . – Joe Oct 04 '14 at 14:51
  • You can't simply because it doesn't make any sense to do so: which object would you be acting upon? The only way you can do anything like this is by passing in instances of the object as parameters. – OMGtechy Oct 04 '14 at 15:05

1 Answers1

1

In oder to invoke an non static method you need to create an object.

Static methods are methods on class level. "normal" methods are on object leven.

so what you need to do in order to executer the non static method is the following:

class ClassName {
   private static void data2() {
       var data1Obj = new ClassName();
       data1Obj.data1();
   }

   private void data1() {
      //execute code here
   }
}

but if you only use data1 in this way you could make that static to

Edo Post
  • 727
  • 7
  • 18
  • For my (and maybe others) knowledge, i would love to know whats wrong with my answer that it deserves an down vote so i can improve this an other answers in the future – Edo Post Oct 04 '14 at 15:19