Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pass Page Variables Into Class Object

Status
Not open for further replies.

jessedh

MIS
Apr 16, 2002
96
US
Hello,

I have several pages that have duplicate functions that look at page level variables (textbox values, etc) and then return values. It is getting burdensome to make sure that all of our changes cascade properly. The logical choice is to incorporate everything into class functions that are called from each page so that we don't have to worry about managing them.

We tried moving some functions into Class objects, but when we try to reference a page object such as me.mytextbox.text we get a compile error.

If anyone has any suggestions it would be incredibly appreciated.

Thanks!
Jesse
 
Pass the object itself to the function in your class. Then reference the properties, methods, etc. as you please

First create a class
Code:
Public Class TestClass
    Function ReturnTBValue(ByVal objTB As TextBox) As String
        Return objTB.Text()

    End Function

End Class

Then from your code behind call the function
Code:
Dim strTest As String
strTest = ReturnTBValue(<TextBoxName>)
 
Use Dim Context As System.Web.HttpContext = System.Web.HttpContext.Current
Context.Session("user_id")
To reference Session vars, Request



Or you can also pass the Me object in your class.

Your ASPX page
Code:
Public Class TestClassAccess
    Inherits System.Web.UI.Page

    Public var As String
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        Dim obj As New Class1(Me)
        Response.Write("var=" & var)
    End Sub

End Class

Your Class

Code:
Public Class Class1
    Private _Me As TestClassAccess
    Public Sub New(ByRef _Page As TestClassAccess)
        _Me = _Page
        _Me.var = "Should work"

    End Sub
End Class

Alternative Class

Code:
Public Class Class1
    Private _Me As Object
    Public Sub New(ByRef _Page As Object)
        _Me = _Page
        _Me.var = "Should work"

    End Sub
End Class
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top