skip to Main Content

Forgive me if I’m doing this wrong according to forum’s rules, but I wasn’t sure how to. This is a revision of the question asked at the link below. There was a response there that was close to an answer, but not exactly. To explain through "comments" or "edits" would have been too much so I thought I’d do a new thread.

How to Share Code Between Pages and Classes

Anyway the (revised) question.

I’m trying to share code between multiple Code Behind pages and multiple Classes that uses objects and methods of the Page class. Simple examples would be using the "response" and "session" objects. However, in my real example I’ll be using other Page objects and methods (e.g. GridView, Panels, etc…).

I’d like to save common Page Object related code in multiple shared Classes and share the code between multiple Pages and Classes.

Example below based on leveraging Albert’s suggestion in my previous (which almost worked… credit and thanks to him).

When using the below I get the error "Object reference not set to an instance of an object" when trying to call "Class2_Procedure" from "Class1".

Example of Page. Calling "Class1_Procedure" and "Class2_Procedure" works. Calling "Class1_Procedure_Calling_Class2_Procedure" does not work.

Partial Class _default
    Inherits System.Web.UI.Page

    Dim MyClass1 As New Class1(Me)
    Dim MyClass2 As New Class2(Me)

    Private Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        Response.Write("Default Page > Page_Load</br>")
        MyClass1.Class1_Procedure()
        MyClass2.Class2_Procedure()
        MyClass1.Class1_Procedure_Calling_Class2_Procedure()
    End Sub
End Class

Class 1. The issue is when you call the "Class2_Procedure" from "Class1".

Public Class Class1
    Dim MyPage As Page

    Sub New(MePage As Page)
        MyPage = MePage
    End Sub

    Dim MyClass2 As New Class2(MyPage)

    Sub Class1_Procedure()
        MyPage.Response.Write("Class1 > Class1_Procedure</br>")
    End Sub

    Sub Class1_Procedure_Calling_Class2_Procedure()
        MyPage.Response.Write("Class1 > Class1_Procedure_Calling_Class2_Procedure</br>")
        MyClass2.Class2_Procedure()
    End Sub
End Class

Class 2. The "Class2_Procedure" can be called from the main page, but the issue is when you call the "Class2_Procedure" from "Class1".

Public Class Class2
    Dim MyPage As Page

    Sub New(MePage As Page)
        MyPage = MePage
    End Sub

    Sub Class2_Procedure()
        MyPage.Response.Write("Class2 > Class2_Procedure</br>")
    End Sub
End Class

3

Answers


  1. You problem is this code:

    Public Class Class1
    
        Dim MyPage As Page
    
        Sub New(MePage As Page)
            MyPage = MePage
    
        End Sub
    
        Dim MyClass2 As New Class2(MyPage)
    

    first up, you probably want ALL of the global scoped variables in the same place. (at the top).

    The 2nd issue, is when you create the instance of the Class 1, the public variables are initialized that THAT point in time.

    So, on "new" event, all of those values are defaulted to their values – but MyPage THEN gets set in the new event. (that looks to be too late for the public Class2 to pick up mypage. (if you converted that to a getter/setter, I think it would thus work.

    So, if you want to "setup" some public members (variables) in that class, you should to do that in the new event – especially in the case that a class you are creating is depending on OTHER values that are public (you can’t control the order here if you use "new").

    Use of new keyword for any public remember in class1 is fine in 99% of cases is just fine, not a issue.

    However WHEN that/those variables (in this case a class instance) are to ALSO use other members/variables?
    Then you don’t have much control here.

    So, force the issue, and make sure then MyPage is "for sure" setup, and then you can with confidence create the new instance of class2, which also requires that Mypage reference.

    So, you need to write it this way:

    Dim MyPage As Page
    Dim MyClass2 As Class2
    
    Sub New(MePage As Page)
    
        MyPage = MePage
        MyClass2 = New Class2(MyPage)
    
    End Sub
    
    Sub Class1_Procedure()
        MyPage.Response.Write("Class1 > Class1_Procedure</br>")
      .etc. .etc...
    

    In other words, "new" keyword looks to be triggering for Class2 before it picks up mypage. (or better said "can" pick up MyPage)

    Login or Signup to reply.
  2. You have to first create the code or the function in a module and then, in the form that you’re working with imports the module or the class, usually it’s windowsapplication.classname.

    Login or Signup to reply.
  3. You can implement a BasePage:

    Public Class BasePage
        Inherits Page
        'Some code here
    End Class
    

    and inherit your code-behinds from BasePage. This way you will be able to use the very same code at multiple pages. You can apply this pattern for other classes as well.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search