.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

Behavior of static classes/variables

Posted By:      Posted Date: September 11, 2010    Points: 0   Category :ASP.Net
Hi, I have a static class, that contains some static DataTable-variables.. In some of them I fill the tables from my database and one of them even does some file-checking..I'm concerned about performance in these variables.. How to they behave? Is it called once in the applications lifetime or everytime I call for the variable?What I mean is, if I have a loop with 500 runs and I use one of the static variables in the loop, is the code that fills the datatable executes 500 times or just once?

View Complete Post

More Related Resource Links

Sharing of Non-Static member variables of a class using .NET Remoting

This kind of weird. I am trying to host an object of a class X (a class library class). This is a SAO and I have set the WellKnownMode as Singleton in the server configuration. <wellknown mode="Singleton" When multiple clients fetch the instance of the class X they can share the static member variables. Any changes made to the static variables are visible to each other. The problem is with the non-static member variables of the class. Each instance is having a local copy of the non-static variables. Why should that happen in case of a Singleton mode? Am I missing something for which this is happening? Any help will be appreciated.  A for apple, B for boy, C for c++

static variables throughout application


hello frnds

i am developing a website in asp.net

i have created a class "Utility" in App_Code Folder

this class contains a static object of another class(LoginInfo) as described below:

public class Utility
public static LoginInfo CurrentUser = new LoginInfo();


This LoginInfo Class is my business object which only contains 2 properties UserID and UserName

Now what i am doing on my login page, after user authentication i am setting these properties with current logged in UserId and UserName

protected void Login_Click(object sender, EventArgs e)

        if (Authenticated User)
        Utility.CurrentUser.UserID = "001"; //ID of current logged in user
        Utility.CurrentUser.UserName = "Admin" //Username of current logged in user;


I am using this CurrentUser object on my webpage to get userid and username.

It works fine, but after few minutes my CurrentUser.UserID and CurrentUser.UserName is set to blank string. 

I am not able to figure out what is the problem. may be due to asp.net recycles.

Please help me how should i solve this problem? Is there any other way?


static variables on asp.net pages


Hello guys. Believe it or now, i have just found that that static objects in classes (pages) hold the same value for that same page even with two different users logged in.

I have a page where the user fills the name and id of a customer (page a) for example and then puts it inside the user session, redirects to page b and on that page writes a report based on that customer and saves it to the database.

We are getting reports that when the first user saves the report, the values of the second user gets saved.

My question is, how to handle the user's data without using static objects inside classes? The click method of button just passes the sender and event arguments, so i cannot pass for example a DataTable through it, and i only use session variables to store information that has already being validated just before sending the user to the next page.

That's why i used static objects, so methods like click inside of buttons could access the data.

Is the only way to do this is to store objects (like a DataTable in my example) in the user's session and then retrieve, update and put it back in the session each time i need to use it? I always though the user's session should be as small as possible.

Static Variables in session


I am creating a static variable and adding to session.This variable has value that is user specific. Now I am getting a problem with this users are complaining that they are getting values that should be another users. It's like a mix up with session. Could this be because of static variable declaration?

Learning C# and OOP, Classes

This is the second lesson in a miniseries designed to teach you how to write object-oriented programs using C#. This miniseries will describe and discuss the necessary and significant aspects of object-oriented programming (OOP) using C#.

Static Class for Beginners

In general, the members of a class can be accessed by instance of that class when they are in public, but there comes a situation to access or call members of a class without an instance and this can be achieved by declaring them as static

Custom Editing Behavior for DataGridView TextBox Columns

For various reasons I'm using a DataGridView instead of a ListView, and while configuring the DataGridView to look like a ListView wasn't much of a problem, there's one thing that got on my nerves, which is the behavior of textbox cells in edit mode: It is much too easy to leave the edit mode accidentally, simply by pressing the cursor keys at the wrong time. For example when the text caret is positioned behind the last character of the textbox cell content, and you press the right arrow key

Temporary tables in SQL Server vs. table variables

When writing T-SQL code, you often need a table in which to store data temporarily when it comes time to execute that code. You have four table options: normal tables, local temporary tables, global temporary tables and table variables. I'll discuss the differences between using temporary tables in SQL Server versus table variables.

InterFaces and Abstract Classes

Link Describes About Abstraction and Interfaces In Breif

MSDN Abstract Classes Link

MSDN Reference For Abstract Clases

abstract away the source of the connection string using a class with a static property.

ASP.NET provides a configuration system we can use to keep our applications flexible at runtime. In this article we will examine some tips and best practices for using the configuration system for the best results.

The element of a web.config file is a place to store connection strings, server names, file paths, and other miscellaneous settings needed by an application to perform work. The items inside appSettings are items that need to be configurable depending upon the environment, for instance, any database connection strings will change as you move your application from a testing and staging server into production.

Let's abstract away the source of the connection string using a class with a static property.

Run Button Behavior: Suggestion/Bug


Note: This could be a bug or considered as suggestion. I am not looking for solution.

1: I have ImageWatermark.cshtml open and currently in focus.

2: I have Photo1.jpg selected in left Files workspace.

3: Now if I Click "Run", instead of opening ImageWatermark.cshtml it trys to open Photo1.jpg and it fails.

So ideally I would expect the "open file (ImageWatermark.cshtml) to run rather than running the selection from Workspace.

Session and Application Variables


I have a web application in which i need to logout the user if he clicks the logout button or even if he closes his browser window. On Logout button click, i am doing session.Abandon(), but how to go about his browser window close.

Can anybody help with a code sample.

NUnit and public static readonly


I have an app that I am testing with NUNit.  The project im testing has several helper classes that are created as public static readonly.  When I run the NUnit tests, they all fail with the same error

SetUp : System.TypeInitializationException : The type initializer for 'Systems.Utils.ConstantHelpers' threw an exception.
  ----> System.NullReferenceException : Object reference not set to an instance of an object.
SetUp : System.TypeInitializationException : The type initializer for 'Systems.Utils.ConstantHelpers' threw an exception.
  ----> System.NullReferenceException : Object reference not set to an instance of an object.

I kept getting this error despite stripping out all

Word Automation - Multiple DCOM Errors/Behavior

Before I get started, I already know that Microsoft does not support and highly discourages server-based MS Office automation.  I've read a multitude of posts and smarmy "*wink*, here's a link" replies, so I'm looking for actual help here, not condescending reprimands.

That notwithstanding, here is my problem.  I have developed a simple method that will open a local Word document, modify some header information, and then save the document as a new file.  As expected, it works great as a console/windows app, but as soon as I went to ASP.NET, DCOM jacked my world up when accessing the app remotely from a different domain account (that has access to the ASP.NET app.  For reference, my method is shown below:


1    public static string ModifyAndSaveBuildDocument(string localFilePath, BuildDocumentType docType, string buildNumber, DateTime buildDate, string projectName)
2            {
3                try
4                {
5                    ProjectConfiguration config = (ProjectConfiguration)ConfigurationManager.GetSection("ProjectConfiguration");

Relation between classes in c# with class diagrams



Can anyone provide me an example of one to one and one two many relationship between classes in c# with class diagram and explationation.

Can IronPython use dynamic variables?


I am building a small application that can

1. Dynamically load 2 text boxes on a web form

2. Build a logic on those textboxes in IronPython to sum them and return the result.

3. I have already built a IPY script that does some opearions on a static text boxes. How to build a script on the dynamic textboxes?

ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend