How to Share Code Between Multiple Pages and Classes

Ptownbro 20 Reputation points
2023-03-18T21:00:20.8733333+00:00

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...).

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

Default 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
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
1,331 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
1,979 questions
{count} votes

Accepted answer
  1. AgaveJoe 20,226 Reputation points
    2023-03-19T12:59:00.1266667+00:00

    Use constructors to control how class members are populated. As written the Class2 member is initialized when Class1 is instantiated but not after the constructor executes. Therefore, the MyPage member of Class2 is null.

    Public Class Class1
        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>")
        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
    
    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
    

    This design creates dependencies which is an approach we try to avoid at all costs. Can you explain the use case?

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Jose Zero 566 Reputation points
    2023-03-19T11:50:52.82+00:00

    You can have Classes, Subs or Functions in separated files that can be used across your application.
    From my experience better leave code that interact with Page in Page Code Behind, and use code in separated files for any other purpose you need like query a database, check value, return a transformed string, etc.

    If you debug your code will notice Page is nothing
    But if you still need your way, make these changes

    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_Procedure1() ' Use new sub
        End Sub
    End Class
    
    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
    
    ' New sub called from Class1, note page as parameter
      Sub Class2_Procedure1(pMyPage As Page)
        pMyPage.Response.Write("<b>Class2 > Class2_Procedure1</br></b>")
      End Sub
    
    1 person found this answer helpful.