Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,

I have a web application in that after login I have stored value of useremail in session as

static string Email=Session["Username"].ToString();


Please let me know can this Email will be common for complete web application.

or let me know what can be scope of static keyword.
Posted
Updated 8-Apr-11 22:14pm
v3
Comments
Wendelius 9-Apr-11 4:14am    
Pre tags added

They're common across the application and not a session.
So as a result, AFAIK, they will be common across the entire instances of the page.
 
Share this answer
 
Comments
Espen Harlinn 10-Apr-11 18:44pm    
Right, my 5 - unless he is running it at a web farm ...
Abhinav S 11-Apr-11 0:20am    
Thank you.
I want to pay attention to correction if the Question.
"Scope of static" keyword is just the object or method marked as static.

Probably, the intended question was "scope of the static field", property or method.
Let's start with the field. The scope of it is the single run-time of the whole single application domain.
The session consideration is only the consequence of this basic fact. Don't mix up the scope with the visibility, accessibility and context. Even though the context is limited to the single class or structure (only one instance for all application domain, not per class or structure instance) and accessibility is limited by access modifiers of the class, structure and the field itself, the scope is the application domain.

With property and method, the idea is slightly different, even though closely related to the notion of static field. (A property can be considered as one or too methods: a setter and a getter.) Static for a method is just an indication that the method is not related to the instance of the class. In syntax, the class qualifier is used, and not instance, because it is irrelevant. In contrast to instance (non-static method) where the reference to the instance is passed to the method through the invisible "this" parameter, the static method cannot operate with the instance object, in particular, it cannot call any non-static members.

—SA
 
Share this answer
 
Comments
Espen Harlinn 10-Apr-11 18:45pm    
Good effort, my 5
Sergey Alexandrovich Kryukov 10-Apr-11 19:10pm    
Thank you, Espen.
--SA
Abhinav S 11-Apr-11 0:19am    
Good answer. My 5.
Sergey Alexandrovich Kryukov 11-Apr-11 0:21am    
Thank you, Abhinav,
--SA
avigodse 11-Apr-11 3:42am    
Even my answer is not totally wrong, and you still TEND to downvote.
Thank You. Keep it up.
Every page is a class and every static variable/method is part of that Class only. As your question, this email will be common for entire application but scoped to the page Class only.

See Static variable means, It will be having onlyone instance of the variable accross all the object of that class.

Better if you want to have some data at application level, have it in Application variable as it is part of AppDomain or you can have your static variable in Global.asax
 
Share this answer
 
Agree with all above answers, It will be more ever like Application variable.
But it's access is purely depended upon the access specifiers.
Even if that field is having 'internal' specified, it will allow to use but will give error at runtime.
 
Share this answer
 
yes that comman in hole application to use session
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900