-3

can I do this from c#:

public static void example()
{
    nonstatic();
}

public void nonstatic()
{ }

if there any work around to this problem please be free to provide it and thank you

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Sora
  • 2,465
  • 18
  • 73
  • 146
  • 3
    `new YourClass().nonstatic()` – I4V Sep 03 '13 at 10:43
  • 3
    Make the static non-static or the non-static static ... – Stefan Steinegger Sep 03 '13 at 10:43
  • Create an instance of your class and call the method. – VahidNaderi Sep 03 '13 at 10:44
  • no, you'll need an instance of the class to access non-static members, see @I4V 's comment. – Jodrell Sep 03 '13 at 10:44
  • 2
    If the method is relevant to a specific instance of the class, then call it on that instance. If it isn't, then why isn't the method static? – Corak Sep 03 '13 at 10:44
  • When you call nonstatic from the static function VS will be saying as an error that it requires an object reference of the class to access the function. – Jibran Khan Sep 03 '13 at 10:48
  • 1
    IMHO, this is not a question one can answer. It is too broad. There is no general solution to this problem. It depends on the specific situation. Is it correct that one method is static, the other is not? On which instance should the non-static method be called? Should it come from a repository, a singleton, an argument? – Stefan Steinegger Sep 03 '13 at 10:49
  • possible duplicate of [Is it possible to call a non-static function inside static function in C#?](http://stackoverflow.com/questions/1053097/is-it-possible-to-call-a-non-static-function-inside-static-function-in-c) – Daniel Kelley Sep 03 '13 at 10:55

5 Answers5

4

static members of a class can be called before an instance of that class exists. Instance members of a class can only be called after an instance of that class exists, and can only be called FROM the instance itself.

A quick work around is just to create a new object of the type you're trying to call:

new SomeClass().nonstatic();

But WHY are you doing that? Is it to make it run? Then you're not writing good code. You're just giving in. Try to push for more elegant solution.. or:

Alternatively, you can ask yourself why your method is static. Does it need to be? Would it hurt to make it non-static or make the other method static? You can avoid these situations with some careful class design.

Edit for completeness

It might be worth calling this static method via an instance of that object, rather than directly from the static method. That way, you don't need to pointlessly create new objects. This is because an instance of a class has access to all of the instance methods, and all of the static methods.

christopher
  • 26,815
  • 5
  • 55
  • 89
  • It might be completely wrong in many cases to create a new instance. IMHO, this is not really a general solution - except to make it compile. – Stefan Steinegger Sep 03 '13 at 10:46
  • Hence why I put in the extra bit. I think you're right though; I'll emphasise it more. – christopher Sep 03 '13 at 10:47
  • 2
    Just for clarity, it's of course perfectly legal to call a static method after an instance exists as well. Static methods are not bound to the lifetime of instances of the class at any way at all. – Dentoid Sep 03 '13 at 10:48
  • I prefer to turn the question on its head. Why can't this method be static? – Jodrell Sep 03 '13 at 10:57
  • Again, good point! I think I covered the concept of redsigning the class before hacking out work arounds, but im sure your comment will clarify. – christopher Sep 03 '13 at 11:06
  • Well, you *could* call a non-static method *without* an instance via reflection (which will throw a predictable `NullReferenceExeption` if you access any instance member in this method). – sloth Sep 03 '13 at 11:18
  • You *could* but let's not get OP into bad habits :) – christopher Sep 03 '13 at 11:19
1

All You need to do is to create an instance of the class and invoke the method on it.

public class Someclass
{

public void Data1()
{
}

public static void Data2()
{
     Someclass foo = new Someclass();
     Someclass.Data1();
}

}
1

Try using Singleton pattern and then call your method on instance of Your object

using System;

public class Singleton
{
   private static Singleton instance;

   private Singleton() {}

   public static Singleton Instance
   {
      get 
      {
         if (instance == null)
         {
            instance = new Singleton();
         }
         return instance;
      }
   }
}

and then

Singleton.Instance.MyNonstaticMethod()
Koscik
  • 190
  • 1
  • 3
  • 14
  • 1
    Indeed the "StaticClass" is very confusing in this context, why not type "Singleton.Instance.MyNonstaticMethod()" ? – Tintenfiisch Sep 03 '13 at 10:48
  • The singleton pattern is rarely the answer you are looking for. – Jodrell Sep 03 '13 at 10:57
  • It happened that I used it few times already, it's useful to get some info from your class in other classes without passing reference – Koscik Sep 03 '13 at 11:02
0

Yes you can do it.

class Example
{
    public static void example()
    {
        new Example().nonstatic();
    }
    public void nonstatic()
    {
    }
    static void main(string[] args)
    {
        example();
    }
}
falsetru
  • 357,413
  • 63
  • 732
  • 636
Vignesh Murugan
  • 575
  • 5
  • 18
0

Based on your example you be better off with this code,

public static void example()
{
    static();
}

public static void static()
{
}

Now, if there was some reason that you needed a non-static member, i.e. some state, you could implement two members.

public static void static(SomeState someState)
{
    // Do something thread safe with someState.
}

public void nonstatic()
{
    static(this.someState);
}
Jodrell
  • 34,946
  • 5
  • 87
  • 124