How To Use Of Static Variables

Mar 9, 2010

I know what they are and how they are used but i am not sure about their feasibility. Their use is always advised to be avoided. can some one explain me when they should be used and when not?

Aif i have a choice between static veriable and session for just storing an integer which one of these should i prefer??

View 3 Replies


Similar Messages:

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

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
{
get
{
[code]...

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

View 2 Replies

C# - Static Variables In WCF?

Feb 13, 2011

I have some WCF services. These services run in ASP.NET. I want these services to be able to access a static variable. My problem is, I'm not sure where the appropriate server level storage mechanism is. I don't want to use the database because of speed. But, I want the static variables to stay in memory as long as possible. In fact, I'd like it to stay until I restart my server if it all possible.

View 2 Replies

Web Forms :: Static Variables Throughout Application

Oct 6, 2010

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:

[Code]....

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

[Code]....

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.

View 3 Replies

Static Variables In IIS-hosted Web Applications?

Jun 27, 2010

If I declare a static field in a type instantiated within an ASP.NET application, hosted within IIS, is the same variable (i.e. same memory location) used by all of the worker threads used by IIS, opening up concurrency issues?

View 1 Replies

Where The Static Variables Are Stored In Aspx Page

Apr 4, 2011

Possible Duplicate: Where are static variables stored in asp.net aspx page Hi can someone please tell me where the static variables are stored in asp.net aspx page.Is it in the view state? If so I guess you wouldn't want to stored big complex objects?

View 1 Replies

Optional Variables In Public Static Void?

Jun 22, 2010

Let's say I have the following;

public static void TryOut(int intOne, int intTwo, string strone, string strtwo){....}

And after I created dll, I will use it like that;

TryOut(1,3,"bla bla bla", "bla bla bla");
But I want the last variable to be optional. I mean this method could be used like that;
TryOut(1,3,"bla bla bla", "bla bla bla");
And Also like that
TryOut(1,3,"bla bla bla");

I know it is possibel but how I could do that I have no idea !

View 5 Replies

C# - Will Static Public Variables In App Get Shared With Other Users In The Same App

Nov 11, 2010

For reasons I would rather not discuss, I need to create a custom authentication system for my app. I was just reviewing the system and am having some doubts if my solution is thread safe. My goal was to create a solution that would allow my app to authenticate a user one time and that users authentication info would be shared by all master pages, pages, classes, user controls, etc that are used. (But not share the same info between users) Here is my setup: PageHttpModule.cs - this is added to the web.config as a httpModule.

public class PageHttpModule : IHttpModule
{
public void Init(HttpApplication app)
{
app.AuthenticateRequest += new EventHandler(OnAuthenticateRequest);
}
public void OnAuthenticateRequest(Object s, EventArgs e)
{
CurrentUser.Initialize();
}
public void Dispose() { }
}
CurrentUser.cs
public static class CurrentUser
{
public static bool IsAuthenticated { get; private set; }
public static string Email {get; set;}
public static string RealName {get; set;
public static string UserId {get; set;}
public static void Initialize()
{
CurrentUser.AuthenticateUser();
}
Note: this is a scaled down version of my authentication code.
public static void AuthenticateUser()
{
UserAuthentication user = new UserAuthentication();
user.AuthenticateUser();
if (user.IsAuthenticated)
{
CurrentUser.IsAuthenticated = true;
CurrentUser.UserId = user.UserId;
CurrentUser.Email = user.Email;
CurrentUser.RealName = user.RealName;
}
}
}
UserAuthentication.cs
public class UserAuthentication
{
public string Email { get; set; }
public string RealName { get; set; }
public string UserId { get; set; }
public bool IsAuthenticated { get; private set; }
public UserAuthentication()
{
IsAuthenticated = false;
Email = String.Empty;
RealName = String.Empty;
UserId = String.Empty;
}
public void AuthenticateUser()
{
//do some logic here.. if the user is ok then
IsAuthenticated = true
Email = address from db
UserId = userid from db;
Realname = name from db;
}
}

I have tested between 3 different browsers and it seems to work fine, but I am still learning and don't want to make a huge mistake. If my logic is totally wrong, then how should I do it so I dont have to put user lookups on every page directly?

View 4 Replies

Static Variables To Be Accessed Across Multiple Instances

Feb 17, 2010

is there any way to stop share the static variable at multiple users....I need to create a new insatnce of static variables or not accessing the same static variables across multiple who are using the same site.....while googling i found like hisSystem.Threading.Interlocked.Increment(ref MyClass.InstanceCounter); by using can I do....or is there any other way to stop accessing the static variables accross multiple instances of my site......and in my scenario i cannot use rely on session variable also

View 2 Replies

DataSource Controls :: Static Variables Vs Linq2sql DataClassesDataContext Objects

May 6, 2010

im developing a silverlight project using silverlight 3, vs2008 and linq2sql. when projects starts, im storing all the data from database into some static list variables. so when ever i need data, im reading it from those static list variables. all i want to know is, is it good to store data in static list variables and use it when ever necessary or is it good to get data directly from DataClassesDataContext object like db.mytable. which is the rite and fastest way or retrieving data. i mean which will use less connections to database?

View 2 Replies

Where Are Static Variables Stored In Aspx Page / Is It In The View State

Apr 4, 2011

where the static variables are stored in asp.net aspx page.Is it in the view state? If so I guess you wouldn't want to stored big complex objects?

View 2 Replies

Accessing Static Variables From Global.asax In Aspx.cs Pages?

Feb 13, 2011

i declared a static variable in global.asax file like below

<%@ Application Language="C#" %>
<script runat="server">
static string Sv;
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
}

i want to access this variable in aspx page. how can i access this variable.

View 2 Replies

Architecture :: Finding Pros And Cons Of Static Classes And Variables (particularly A Db Connection)

Jan 11, 2010

So I started working on my first asp.net application that involves logging in and databases, and soon after i started messing around with a static class. I "discovered" that if you make a variable static, all sessions share that variable (I for some reason was originally assuming that each session had its own copy of that "static" class). Anyway, after discovering this I thought to myself "how could this possibly be useful to me" and thought that itmight be a good idea to make a single static database connection for all of the sessions, rather than storing that as a session variable for each session. Does anybody know what would be the pros and cons of this approach?

View 5 Replies

Entity Framework - Is Using Public Static Variables For Compiled Queries Bad In Applications?

Aug 19, 2010

I'm coding a business layer for an ASP.NET application. I've created database methods in my BLL as static. I've created public static Func variables to be compiled and used in several different methods, like this:

namespace BLL
public class User
{
public static Func<Context, variable, result> selectUser;
private static void CompileQuery()
{
if(selectUser == null)
{
selectUser = CompiledQuery.Compile......
}
}
public static UserClass Select(int id)
{
CompileQuery();
//uses selectUser
}
public static SomethingElse DoSomethingElse()
{
CompileQuery();
//also uses selectUser
}
}

It'll be used in ASP.NET layer like this: using BLL;

private void AddUser()
{
UserClass user = User.Select(id);
}

My question is, since static variables are not thread-safe, is this a bad design decision? I'm thinking of either implementing a locking mechanism, which makes me think if it'd slow down the application, or using instantiated class approach which makes me wonder if query compiling would be beneficial.

View 1 Replies

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.

DETAILS:

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

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().

[Code]....

[Code]....

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

Collect 2 Variables From One Hyperlink, And Use Those Variables In The Page_load To Set As Session?

Aug 8, 2010

i am trying to collect 2 variables from one hyperlink, and use those variables in the page_load to set as session. but i don't know how to collect those 2 variables

such :

<a href="javascript:;" onclick="wsChangeColor('mainData', '#FF0000','#FFE4E1');return false;" title="Change color" id="red">1</a>

i would like to collect '#FF0000','#FFE4E1' or direct set '#FF0000','#FFE4E1' to string then send to pageload, how can i do this by only clicking on it ?

View 1 Replies

Web Forms :: Storing Javascript Variables Into C# Variables

Jan 2, 2010

I have this code in javascript: var x = e.mapX; It gets the X-coordinate of a map. What I want to do is that I want to store this into a c# variable. I have a class named Test with an integer property X. I want to store var x into X. In the codebehind, I have this on the Page_Load: Test test = new Test(); Then I am trying this on the javascript code: var x = e.mapX;

View 16 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

What Is The Point Of Static

Feb 23, 2010

explain to me the point of static?

Say I have a class called GenericFunctions and a function in there called FormatAddress.

If I want to access that function I have to do this:

Code:

GenericFunctions GF = new GenericFunctions();
string FormattedAddress = GF.FormatAddress(int AddressID);
If I make FormatAddress static I can access it without creating an instance of the class by just calling

Code:

string FormattedAddress = GenericFunctions.FormatAddress(int AddressID);
So, making the function Static means I don't have to create an instance of the class. So, what's the point? Why not make everything Static?

View 30 Replies

How To Add Static Values In Datalist.

Jun 1, 2010

how i add static the values with pdf image in datalist on asp.net (C#) web page.

for example i want to add the following values in datalist with pdf image.

Download Application {Pdf. image here}
Download Claim Form {Pdf. image here}
Download Clearance Report {Pdf. image here}

View 17 Replies

C# - Static Methods For Biz Layer?

Nov 18, 2010

Actually I have done all my Biz(business layer) and DAL CRUD Opprations using static methodes
and I just send my error messages to my log table a sample of biz layer

public static bool Delete(Guid LogGroupID)
{
using (DAL.ChroXEntities db = new ChroX.DAL.ChroXEntities())
{
var q = (from lg in db.LogGroupSet

[code]...

so what should i do to propagate an user friendly error to my users?

View 1 Replies

Security :: Trying To Understand Static?

Mar 30, 2011

If I have a singleton pattern as follows:

[Code]....

So Let's say User A is logges in first and does a following call:

[Code]....

View 2 Replies

Why Ajax Pagemethods Are Static

Aug 18, 2010

Why Ajax pagemethods are static?

View 1 Replies







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