470,588 Members | 2,141 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,588 developers. It's quick & easy.

Private variable of user made class not accessible within own asp.net page

I've done some searching but haven't had much luck figuring this one out - hopefully someone here has some insight as to what I might be doing wrong.

Assuming we have a regular C# driven asp.net website as follows:

Expand|Select|Wrap|Line Numbers
  1. public partial class Example : System.Web.UI.Page
  2. {
  3. private int _testInt;
  4. private CustomClass _testClass;
  6. protected void Page_Load(object sender, EventArgs e)
  7. {
  8. _testInt = 5;
  9. _testClass = new CustomClass;
  10. _testClass.name = "test"; <-- assumes class has all get/set, etc
  11. RunMe();
  12. }
  14. protected void RunMe()
  15. {
  16. Response.Write(_testInt);
  17. Response.Write(_testClass.name);
  18. }
  20. }
I just threw the above together without a IDE so there might be some mistakes but it serves to illustrate my questions. I've included two private variables of type int and one for a user made class. I have no problem accessing the private int from anywhere on the page, including after postbacks, events, etc. But for some reason the user made class object is inaccessible beyond the immediate Page_Load or wherever else it was initally instanced. In this example for instance, RunMe will output the int but not the class - it also doesn't seem to recognize the class as being a instance as well.

While I didn't include what CustomClass does it really doesn't matter. The example assumes that the class is referenced within the website project and the proper "using" tags are on the actual asp.net page.

I'm hoping I'm explaining the situation clearly enough as I'm having a hard time explaining the situation.

Hopefully someone can help and thanks.
Aug 24 '07 #1
1 1274
4,871 Expert 4TB
Have you tried making _testClass = new CustomClass; global and accessible everywhere? To achieve this you just need to move it up the page. And try passing the values when you call runme. HTH.
Aug 26 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

8 posts views Thread by Dave | last post: by
2 posts views Thread by Christoph Boget | last post: by
3 posts views Thread by Tim Zych | last post: by
41 posts views Thread by Miguel Dias Moura | last post: by
11 posts views Thread by prefersgolfing | last post: by
reply views Thread by Luis Zarrabeitia | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.