In ASP.NET with C#, what is the proper way to store class instances between postbacks? Storing the class instance in a Session variable sounded like the way to go for a class that contains properties specific to a user, but I couldn't get it to work. Is this
correct below? // Store the class instance in a session varible. MyClass myClassInstance = new MyClass (); Session["myClassInstance "] = myClassInstance; // Call a method in the class instance. Session["myClassInstance "].myMethod( param1, param2); When I
refer to it this way I get a compile error that "myClassInstance" isn't an object with a definition for myMethod. Can someone give me an C# example of how to store your class instances? Or point me in the right direction? I was able to store a class instance
by setting it in the Global.asax. That works great, but then I assume the user specific data it stores is being stored for all users of the application or there would be a conflict? In other words to wide of a scope for a class storing and manipulating user
specific data. Thanks very much for any help.
You need to cast the session object back to its type: MyClass myClassInstance = (MyClass) Session["myClassInstance "]; myClassInstance.myMethod( param1, param2);
Thanks, Paul Wilson, ASPInsider, MC**
For the best .NET code, examples, and tools, visit:
WilsonDotNet.com, WilsonWebPortal.com, ORMapper.net
zephyros
Participant
1045 Points
209 Posts
Storing class instances in a session variable?
Dec 04, 2003 06:10 PM|LINK
PaulWilson
Contributor
3715 Points
745 Posts
ASPInsiders
Re: Storing class instances in a session variable?
Dec 04, 2003 06:28 PM|LINK
For the best .NET code, examples, and tools, visit:
WilsonDotNet.com, WilsonWebPortal.com, ORMapper.net
Colt
All-Star
15569 Points
1986 Posts
ASPInsiders
MVP
Re: Storing class instances in a session variable?
Dec 05, 2003 02:32 AM|LINK
zephyros
Participant
1045 Points
209 Posts
Re: Storing class instances in a session variable?
Dec 05, 2003 05:41 PM|LINK