Connecting Tech Pros Worldwide Forums | Help | Site Map

Global variables in VC#.net windows application

Newbie
 
Join Date: Dec 2006
Posts: 21
#1: Jan 27 '07
Hi!
I want to use some variables throughout my VC#.net windows based application...........
How can i declare global variables that will work throughout my project...
-Regards

kenobewan's Avatar
Moderator
 
Join Date: Dec 2006
Posts: 4,745
#2: Jan 28 '07

re: Global variables in VC#.net windows application


I recommend declaring them in the application_start event of the global.asax file. This article may help if you are unfamiliar with the ASP.NET application file:
Global.asax Syntax
almaz's Avatar
Expert
 
Join Date: Dec 2006
Location: Kyiv, Ukraine
Posts: 167
#3: Jan 29 '07

re: Global variables in VC#.net windows application


Quote:

Originally Posted by rhyme2ri2

Hi!
I want to use some variables throughout my VC#.net windows based application...........
How can i declare global variables that will work throughout my project...
-Regards

Global variables, especially those being updated from various parts of application, make your code unreadable. General thoughts that should be considered when you want to declare something to be globally available:
  • Each variable is bound to some functionality. Thus, it is better to put it to corresponding class as static/instance member, so it will be maintained through methods/properties of that class.
  • Static members containing common values that are changed from different places of application can brake encapsulation rule.
Nevertheless it may be useful to separate common constants/settings to a separate internal static class, something like this:
Expand|Select|Wrap|Line Numbers
  1. internal static class Constants
  2. {
  3.     public static int UndefinedID = int.MinValue;
  4.  
  5.     public bool IsDefined(int id) { return id != UndefinedID; }
  6. }
  7.  
Needs Regular Fix
 
Join Date: Jul 2006
Location: India,Hyderabad
Posts: 367
#4: Jan 30 '07

re: Global variables in VC#.net windows application


hello kenobewan

i want to how you can have a global.asax file in windows based application

as you have said in the above reply.


please tell me if its possible it is very use full for me too.


regards
NMsreddi
Reply