C# - How To Lock A Private Static Field Of A Class In One Static Method

Mar 26, 2011

I have a private static field in my Controller class in an MVC web application.

I have a static method in that controller that assigns some value to that static field, I want to apply lock on that static field until some other instance method in the controller uses the value stored in the static field and then releases it.


I have a controller named BaseController having a static ClientId field as follows and two methods as follows:-

public static string ClientId = "";
static void OnClientConnected(string clientId, ref Dictionary<string, object> list)
list.Add("a", "b");
// I want the ClientId to be locked here, so that it can not be accessed by other requests coming to the server and wait for ClientId to be released:-
BaseController.clientId = clientId;
public ActionResult Handler()
if (something)
// use the static ClientId here
// Release the ClientId here, so it can now be used by other web requests coming to the server.
return View();

View 1 Replies

Similar Messages:

AJAX :: Calling Non-static Methods From A Static Method?

Jun 30, 2010

I did some research after posting. All I found was simple examples for no-layer architectures, like connecting to a database from your aspx page, so, in a corporate environment, it is unnaceptable.

I need to call a server-side method (using ASP.NET Ajax) in a 3-layer architecture.

For example, my Default.aspx contains a method LoadProducts().



This cannot change. There is no way to convert Business and Data layers to static.

How can I call the LoadProducts() method using ASP.NET Ajax?

View 2 Replies

C# - How To Access Singleton Class's Static Method

Nov 17, 2010

I have some confusion with singleton class, below are my some points:

1.Can singleton class have static method?,if yes then how we call that methods?
2.what is main difference between Static class and Singleton Class?

I have created my singleton class as follows:


In Above class structure I have created two method one is Static and second is non static, When I am trying to access Static Method it gives me compile time error.

How can I use static method of singleton class?

View 7 Replies

C# - Get The Name Of The Class Without Instantiating Object Or Having A Static Method?

Feb 22, 2011

I hate to see the name of the class used as a string parameter like "FileDownloader" in the code, and I would like to use something like this FileDownloader.Name(), where FileDownloader is name of the class.
Only problem is that I can't find out how to do that without instantiating object or creating a static method...

Is there a way to get a class name in .net without having the object instance and without creating a static method that returns the name of the class?

View 3 Replies

C# - What Is The Life Span Of A Field In A Static Class

Feb 2, 2011

I have a simple web service, in it i have a static class which has a static collection to remember alive tokens.

I just want to make sure that the token collection lives until the next iisreset or the application pool recycles.

And what is the difference between remembering states in Application bag and static class?

View 1 Replies

Web Forms :: Accessing Non-Static Method From Another Class Without Instantiate

Apr 27, 2016

I have a class and call class A method by creating class A object

unlike this call Class B method by using class B object 

Code is here below

class A {
public void A1() {
Console.WriteLine("I am from Class A");
class B {
public void B1() {
Console.WriteLine("I am from Class B");

[Code] .....

View 1 Replies

Public Static Variable Declared In Public Static Class MyGlobals In A 'Code File' Template?

Jan 27, 2011

I have a question about C Sharp ASP.NET:Is there a difference (in code speed, resources) between:public static variable declared in public static class MyGlobals in a 'Code File' template;and the variable declared in a normal 'Class File' template;I use this variable in 2 different Class Files and also in _Default Page codebehind cs file.In fact in my case I need about 20 global variables of type List<string>.

View 9 Replies

Web Forms :: Object Reference Is Required For Non-static Field / Method Or Property

May 7, 2015

I'm using jQuery and setInterval method in my asp.net web application.

I call the webservice every 5 seconds in order to check for DB changes. When i see a change, i want to change the photo of the linkButton that located on the master page, but i can to that due to static constrains.

An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.Master.get'

my c# code is:

public static bool checkDBChange(string userId) {
DBConnection dbConnection = new DBConnection();
if (dbConnection.isChanged(userId)) {
((Site1)Master).SetImageUrl = "~/Icons/a.ico";
return false;

The SetImageUrl is a setter in my master page that sets the new url to the linkButton. How I can implement it?

View 1 Replies

CS0120 Error - An Object Reference Is Required For The Non-static Field, Method, Or Property?

Mar 24, 2010

I am getting a CS0120 error when trying turn a button visible after checking some variables. In plain english, If AmmohelpSession.UserActions contains AmmohelpEnums.UserAction.ArticleEdit then turn the Edit button visible. Here is my comparison:


In the code behind for AmmohelpSession, a public class AmmohelpSession which contains a private variable: private HashSet<UserAction> _userActions; has been stated. In that same file, there is a public function for the UserActions that does a get or set method.In the code behind for AmmohelpEnums, we are setting byte variables to specific actions. Mine would be something like:


Where is my error coming from ?????

View 1 Replies

Alternative For Static Method In Interface - Enforce Implementing Class-level Methods

Oct 28, 2010

I have a hierarchy in my website project as below:

[CustomControl1 - folder]
- CustomControl1.ascx
- CustomControl1.css
- CustomControl1.js

I load css and js files dynamicaly based on which controls are used on particular page. I am doing it by using following code:


where AddLinks method adds HtmlLink controls to Page.Header with href attribute set to coresponding css and/or js file.

I would like to add Interface that would force new controls to have AddLinks method but it is impossible since it is a static method. Because my custom controls inherit from Control class I cannot use abstract class and/or virtual methods either. How can I achieve my goal?

View 1 Replies

State Management :: Clear Private Static Collections?

Aug 9, 2010

I am working on rewriting an application. The previous developers have declared some Private static hashtables within a class in a class library. The first time when the application runs, this hashtable gets populated by running the query. After that this hastable is refrenced elsewhere to get the employee information.Even if the employee is removed from the DB, he is still able access the application, Since the private static hashtable holds the employee information. I totally understand that the hashtable is referenced to prevent the DB calls everytime.

The problem is even after closing the browser and restarting the application the hashtable holds the employee information. How do i get clear this so that it calls the DB?

View 9 Replies

C# - Is It Safe To Access .net Session Variables Through Static Properties Of A Static Object

May 10, 2010

Is it safe to access asp.net session variables through static properties of a static object?Here is what I mean:

public static class SessionHelper
public static int Age

Is it possible that userA could access userB's session data this way?

View 2 Replies

C# - How To Bind "OnDataBound" Event Of "DropDownList" In Declarative Syntax To A Static Method In Some Other Class

Jun 16, 2010

How to bind "OnDataBound" event of "DropDownList" in declarative syntax to a static method in some other class ?e.g<asp:DropDownList runat="server" id="d1" OnDataBound="SomeOtherClassThanThisPage.StaticMethod"></asp:DropDownList>

This will give the error, "Page does not contain a definition for SomeOtherClassThanThisPage. it like this or it be done in the Code Behind only ?

View 2 Replies

C# - Static Methods Updating A Dictionary<T,U> Is It Safe To Lock() On The Dictionary Itself

Dec 23, 2010

I have a class that maintains a static dictionary of cached lookup results from my domain controller - users' given names and e-mails.My code looks something like:

private static Dictionary<string, string> emailCache = new Dictionary<string, string>();
protected string GetUserEmail(string accountName)
if (emailCache.ContainsKey(accountName))
lock(/* something */)
if (emailCache.ContainsKey(accountName))

View 6 Replies

Configuration :: Field Initializer Cannot Reference The Non-static Field?

Feb 3, 2011

when i am declaring my connection string i am getting error below is my connection string


the error is A field initializer cannot reference the non-static field, method, or property 'Default3.con1' .i am unable to type con1.

View 2 Replies

Static Class In App_Code?

Jan 8, 2010

If a static class is put in App_Code, does that imply only one instance will be created, shared by different http requests?Or each request still brings one request?

View 3 Replies

How To Assign A Value From A C# Static Method To A Label

Feb 5, 2010

I have the following static function in c#

public static string Greet(string name)
string greeting = "welcome ";
// is it possible to pass this value to a label outside this static method?
string concat = string.Concat(greeting, name);
Label1.text = concat;
//I want to return only the name
return name;

As you can see in the comments, I want to retain only the name as the return value, however I want to be able to take out the value of the concat variable to asign it to a label, but when i try the compiler refuses, can it be done? Is there a work around?

View 4 Replies

Use A Static DbUtil Class For Web Is It Dangerous?

Oct 4, 2010

is it ok to use something like this in the web:
(my application is on asp.net mvc)

public static class DbUtil
public static int Insert(object o, string cs)
using (var conn = new SqlConnection(cs))
using (var cmd = conn.CreateCommand())
return Convert.ToInt32(cmd.ExecuteScalar());


View 2 Replies

MVC :: Accessing An Existing Static Class?

Mar 15, 2011

So i have some existing functionality from an older ASP.Net app that is a static class.

Say there's a static method in that class that returns a dataset that I would like to bind to something like a gridview or whatever I can build using MVC3.

View 2 Replies

C# - Access Static Class From Form?

May 10, 2010

I am using <%# MyFormatClass(Eval("fieldname")) %> to display data in a grid view on my page. MyFormatClass works fine, but now I want to move it to a central location that can be used by many pages. When I try to access the class <%# Utils.MyFormatClass(Eval("fieldname")) %> it no longer works. Is this something we are allowed to do?

View 1 Replies

Working With Static Variables / Store The User Specific Information In Static Variables?

Mar 5, 2011

whats the exact use of static variables in overall programming in .net and for asp.net...

Recently i went for the interview where interviewer asked me 2 question which i was not sure for the same..

whats the use of session object, i said sessions are the server side object, they are used when you want to store user specific data at server side, then he asked what if i want to use static variables for the same, i was mum, can anyone tell me how asp.net will behave if i store the user specific information in static variables.If i use cookies which are the best option to store the data at client side (not sensitive one), but what if user has disabled cookies on his machine, will my application would crash.

View 3 Replies

Static Method Can Be Declared Inside Interface?

Apr 4, 2010

Can we declare a static method inside the Interface?If not means Why?

View 4 Replies

Javascript - Access Gridview In Static Method?

Sep 1, 2010

I want to access my gridview in the static method? I am doing async call from javascript, I need code for this,

public static void bindGrid(string userinfoId, int row)
GridView gv;
gv = (GridView)gvParent.Rows[row - 1].FindControl("gvChild");
gv.DataSource = GetProfileData(userinfoId);

i want to access grid view,gridview is not static. I want to know, Is their any possiblity for using gridview in static method.? Yes or No.

View 1 Replies

How To Get Classes To Expose The Same Shared / Static Method

Mar 4, 2010

I have a base class with several derived classes. I want all of my derived classes to have the same Public Shared (static) method with their own implementation. How do I do this?

View 3 Replies

C# - Control Reference Static Method Performance?

Jun 10, 2010

I have just asked which one is better?Static Vs Non-Static? [URL]I would like to take this discussion one step ahead.Consider If i pass reference of Panel control as parameter to Public static method, will static method still rules in performance?

View 1 Replies

Copyrights 2005-15 www.BigResource.com, All rights reserved