How to access a page level variable from within a class instance


Corey B

Is there a way for an instance of a custom class to access an ASPX page
level variable? I know that I can access a Session variable from
within a class using the following code:

myClassVar =

But I can't figure out if it is possible to access a page level
variable from within a class.

For example, let's say I had the following code for my ASPX page:

Public Class myPage
Inherits System.Web.UI.Page

Public myPageVar as String

Private Sub Page_Load (ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
myPageVar = "My Test"
Dim myObject as New myClass()

End Sub
End Class

And that I had the following code for a custom class:

Public Class myClass

Private _localVar as String

Public Sub New()
_localVar = ????????? (How do I access the variable myPageVar on
the ASPX page?)
End Sub

End Class

Any help would be greatly appreciated.


bruce barker \(\)

you have a couple options.

1) pass the variable on the constructor
2) have the aspx page implement an interface, and pass the interface to
3) pass the page instance to the class and use reflection

-- bruce (

Corey B

Yeah I was trying to avoid passing anything to the class (either in the
constructor or otherwise). I am implementing my own XML serialization
for the class and the serialization engine seems to need a
parameterless constructor for the serialization to work. I just wasn't
sure if there was some built in way that a class could reference a
variable on the page where the class was instantiated.


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question