 |
| General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category.
** PLEASE BE SPECIFIC WITH YOUR QUESTION **
When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

December 15th, 2004, 04:24 AM
|
|
Authorized User
|
|
Join Date: Aug 2003
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Persistent Classes??
Hi:
I'm working on an ASP.NET app. in C#
I've got a class - SvrFunctions.cs
This class has all static vars and methods.
I was under the impression that the class is instantiated every time my Web page loads.
This is clearly not true.
I loaded an ArrayList in SvrFunctions and displayed it on my page.
I CLOSED THE BROWSER and reopened it and the app. remembered the values I had put into the ArrayList.
I'm getting all kinds of weird stuff. It seems to persist sometimes and sometimes not.
What's going on? Is there any way I can control it?
|
|

December 15th, 2004, 06:15 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Static variables are available for the life of the application, not a single page.
For the static variables to be reset, you'll need to restart either IIS or the application (for example, by making a change to the Web.config).
Cheers,
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|
|

December 15th, 2004, 06:19 AM
|
|
Authorized User
|
|
Join Date: Aug 2003
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Ah, I see thanks.
I may need to change them so the page must instantiate an object.
You say 'application'. Does that mean they're persistent from one user to another as well?
That is, if one user changes a static variable in my class, does the other user see the changes?
|
|

December 15th, 2004, 06:26 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Yes. You should see static variables as defined on the class, not on an actual instantiated object.
You could, for example, keep a user hit counter in the global.asax file using static variables.
Imar
|
|

December 15th, 2004, 06:29 AM
|
|
Authorized User
|
|
Join Date: Aug 2003
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Very Interesting.
I've got some recoding to do but no big deal.
If I opened a database connection using a static variable, I'd only have to do that once for all users for the entire application? (not that I'm sold on that technique, but I could?).
Or I could post some dynamically set generic text somewhere for all users to see? It looks like I could use something like that for a chat session or something.
|
|

December 15th, 2004, 07:29 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
I suppose you could, but this is a *very* bad idea. This basically means your entire application uses only one connection, which will slow down or crash your application when you have more than a handful of users.
When dealing with connections, use the Bankrobber's 3G rule: Go in, Get what you want and Get out. This means: Open a connection as a late as possible, get or send the data, and close the connection right away.
So, on each page open a connection object and immediately close it again when you're done with it. Connection pooling will take care of handling your connections resulting in improved performance.
Single-Connection applications are better suited for desktop scenarios. That is, with a Windows Forms application, or an Access app where you have a direct and continuous connection to the database.
Personally, I would never store the type of info you mentioned in static variables. Other global repositories, like the Cache, Application state, or even better, a database are much better suited for these kind of tasks. Storing data in the database allows you to work with the data, even when the application is restarted through IIS, Process Recycling or changes to the application's .Config file.
HtH,
Imar
|
|

December 15th, 2004, 03:35 PM
|
|
Authorized User
|
|
Join Date: Aug 2003
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Yes, thanks.
I've already removed the static from all my database connections.
It just amused me that I could.
But, I could use it to do something like maintain a room?
|
|

December 15th, 2004, 03:51 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
As I said in my previous post, *I* wouldn't do it as it isn't a reliable / persistent data storage. As soon as your application restarts (this may be more frequent than you realize) your data is lost.
IMHO, there are better and more reliable data stores than static members. You can't really rely on the value of the members being present (or still containing the latest version of the value) after the application has been restarted.
Cheers,
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Boom! by System Of A Down (Track 4 from the album: Steal This Album) What's This?
|
|

December 15th, 2004, 04:00 PM
|
|
Authorized User
|
|
Join Date: Aug 2003
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Oh, I see. I was thinking you were talking about database connections in general.
I didn't really have a chat room on the table right now.
If I do one, I'll take your advise and NOT use static variables.
Thanks much:o)
|
|
 |