none
How can I declare and instantiate an object at the *same* time in an aspx page when the object constructor requires the page context? RRS feed

  • Question

  • Example based on an aspx page 

    I need to instantiate an object that has a constructor that requires an instance of the System.Web.UI.Page, but this object cannot be instantiated at the point of its declaration because the 'this' context is not available.

     public class MyClass
        {
            public MyClass(System.Web.UI.Page page)
            //...
        }
        public class MyPage : System.Web.UI.Page
        {
            private MyClass myClassWorks; // Compiles but is not instantiated...
            private MyClass myClassFails = new MyClass(this); // Obviously fails because 'this' is not valid...
            protected void Page_Load(object sender, EventArgs e)
            {
                myClassWorks.DoSomething(); // Throws null exception because I have forgotten to instantiate it
                                            // This is the error I want to prevent
            }
        }

    A guess at a solution?  Is there any way that I can create a parameterless constructor for MyClass that can be used to 'register' the existence of the MyClass class in some way so that the System.Web.UI.Page can be assigned later, but before the MyClass instance is used? I thought about sub-classing the Page object (i.e. a MyExtendedPage object) that performs a Page_Load and then includes a virtual Instantiate_MyClasses function.  For a windows App a class static List<MyClass> would have worked just fine to keep a list of the instances, but aspx static variables are Application level and Session 'variables' involve a ViewState, neither would be acceptable.

    Any ideas?


    Monday, March 19, 2018 5:37 PM

Answers

  • Your MyClass should be implementing and Interface and instanced in an IoC container. 

    The class constructor for the form, should use the IMyClass so that the IoC can dependency inject Myclass object that has been instanced by the IoC into the form class. 

    You should learn how to use Unity,  Castle Windsor, Nject and many other IoC(s) that can be used in an ASP.NET Web form solution.

    ASP.NET can be discussed at the ASP.NET forums.

    http://forums.asp.net/

    • Marked as answer by Datamast Tuesday, March 20, 2018 8:29 AM
    Monday, March 19, 2018 10:02 PM

All replies

  • Why you need System.Web.UI.Page object in your class? Cannot you instantiate it when Page_Load?

    private MyClass myClassFails;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        myClassFails = new MyClass(this);
        myClassFails.DoSomething();
    }
    
    

    Or you can create property and null check in it.

    private MyClass myClassFails = null;
    
    private MyClass MyClassFails
    {
        get 
        {
            if (myClassFails == null)
              myClassFails = new MyClass(this);
            return myClassFails;
        }
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
        MyClassFails.DoSomething();
    }

    Monday, March 19, 2018 6:18 PM
  • I am trying to avoid a programming error where 'MyClass' is declared but not instantiated.  A compiler never warns you, the only warning you get is when the runtime code generates a null based exception.  If I was writing this for a Windows App, then I can *ensure* that all declared MyClasses are instantiated automatically and I will never get an error whenever I attempt to use it.

    In reality MyClass generates the JavaScript necessary to support ModalDialogs on a aspx page. It's so easy to declare a MyClass when several are being used and then forget to instantiate one of them when needed.

    I like to design code that 'forces' me (using Virtual functions etc) to code in such a way that errors are minimised by making them compile time errors. Obviously I only do so on more complex classes that require some context to operate.


    Monday, March 19, 2018 7:53 PM
  • Your MyClass should be implementing and Interface and instanced in an IoC container. 

    The class constructor for the form, should use the IMyClass so that the IoC can dependency inject Myclass object that has been instanced by the IoC into the form class. 

    You should learn how to use Unity,  Castle Windsor, Nject and many other IoC(s) that can be used in an ASP.NET Web form solution.

    ASP.NET can be discussed at the ASP.NET forums.

    http://forums.asp.net/

    • Marked as answer by Datamast Tuesday, March 20, 2018 8:29 AM
    Monday, March 19, 2018 10:02 PM