0

Want to access non-static method from static method in asp.net c#.

private void nonstaicMethod(string var1)
{
  //accessing the view/panel from the current page
  //or using 'Response' to download a file
}
[System.Web.Services.WebMethod(EnableSession = true)]
public static void method1(string Id)
{
  CurrentPageName cp=new CurrentPageName();
  cp.nonstaicMethod();
}

For the downloading file, I got response is not available in this context. I have tried,

 <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />

 `HttpContext context = HttpContext.Current`;

tried many ways when I gone through net. Nothing works. When I tried to call nonstatic and accessing view/panel from the current page, I got Object reference not set to an instance of an object.

Adrita Sharma
  • 21,581
  • 10
  • 69
  • 79
user8158183
  • 86
  • 2
  • 12
  • Just make the static method, non-static. – leppie Sep 20 '17 at 04:26
  • Here are a lot more great answers: [How do I call a non-static method from a static method in C#?](https://stackoverflow.com/questions/1360183/how-do-i-call-a-non-static-method-from-a-static-method-in-c/32610084#32610084) ... and most likely make this post a duplicate. Also the below given answer's code sample appears to be taken from an existing answer at this link. – Asons May 24 '21 at 12:23

1 Answers1

0

it looks really wrong to put behavior in something in a page and attempt to call it from elsewhere. Perhaps it should be in another class and take a Page instance as a dependency if required. Here this whole pattern screams there is something wrong and I would avoid it even when possible.However 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();
        }
    }
}
lazydeveloper
  • 891
  • 10
  • 20
  • If i need to pass parameter in this,(MyClass.NonStaticDelegate = new Action(temp.NonStaticMethod);) What should i do – user8158183 Sep 20 '17 at 04:45
  • It enters into my nonstatic method,but still getting "Object reference not set to an instance of an object." when accessing the panel/view of the current page – user8158183 Sep 20 '17 at 04:52
  • Action is used only for void return and parameter less methods namespace System { // // Summary: // Encapsulates a method that has no parameters and does not return a value. [TypeForwardedFrom("System.Core, Version=3.5.0.0, Culture=Neutral, PublicKeyToken=b77a5c561934e089")] public delegate void Action(); } – lazydeveloper Sep 20 '17 at 17:58
  • I believe calling non static method from static method is first thing in programming ..you could have learn.We call non static method by class reference in Main(string[] args) function in Program.cs – lazydeveloper Sep 20 '17 at 18:14