473,387 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Good Way to declare global variables in C# NET

Hi,

Hi, currently I developing an application in C# NET, let say i have a page call

viewrecord.aspx, in this page, i have a global variables declared before the page_Load, so all function can access the variables.

example

public string record;
public int status;
... etc..

But I found this is not a good way,
do you guys have other way to declare global variables, and not to declare in the page (upper part)?
May 22 '07 #1
9 29632
Plater
7,872 Expert 4TB
So this is a web application?
As a web application you can use web.config to store global varriables.
You should look up the [HTML]<appSettings>[/HTML] tag for it. There is good help on it.

I also like to create a static wrapper class for my web.config variables to make accessing them easier.
Expand|Select|Wrap|Line Numbers
  1. public static class GlobalVars
  2. {
  3.     private static AppSettingsReader ap = new AppSettingsReader();
  4.     public static bool isDebug
  5.     {
  6.         get
  7.         {
  8.             return System.Diagnostics.Debugger.IsAttached;
  9.         }
  10.     }
  11.  
  12.    public static string PDFBaseDir
  13.    {
  14.       get
  15.       {
  16.          return ap.GetValue("PDFBaseDir", typeof(string)).ToString();
  17.       }
  18.    }
  19.  
May 22 '07 #2
Frinavale
9,735 Expert Mod 8TB
Hi,

Hi, currently I developing an application in C# NET, let say i have a page call

viewrecord.aspx, in this page, i have a global variables declared before the page_Load, so all function can access the variables.

example

public string record;
public int status;
... etc..

But I found this is not a good way,
do you guys have other way to declare global variables, and not to declare in the page (upper part)?
Why is this not working for you?

A global variable declared in this way will only be used by the functions within that page.

If you are looking to use this variable on other pages you'll have to store it in Session, or in the <appSettings> tag in your web.config file like Plater suggested.

What problems are you experiencing?

-Frinny
May 22 '07 #3
Motoma
3,237 Expert 2GB
Hi,

Hi, currently I developing an application in C# NET, let say i have a page call

viewrecord.aspx, in this page, i have a global variables declared before the page_Load, so all function can access the variables.

example

public string record;
public int status;
... etc..

But I found this is not a good way,
do you guys have other way to declare global variables, and not to declare in the page (upper part)?
You should also look up the static keyword.
May 22 '07 #4
Plater
7,872 Expert 4TB
I left out the web.config section that matches my code, and since I don't seem to be able to edit the post:

[HTML]
<appSettings>
<!-- Generic Settings -->
<add key="PDFBaseDir" value="\\server\pdfs\"/>
</appSettings>
[/HTML]
May 22 '07 #5
Hi thank for replied.


because i need a variabled which can become value=0 or value=1.

but i heard it is not good to declare global variables within the page


Why is this not working for you?

A global variable declared in this way will only be used by the functions within that page.

If you are looking to use this variable on other pages you'll have to store it in Session, or in the <appSettings> tag in your web.config file like Plater suggested.

What problems are you experiencing?

-Frinny
May 23 '07 #6
Plater
7,872 Expert 4TB
In my compsci classes in college they always told us not to use global varriables. "Pass your varriables to your functions" they said. I think they didn't know what they were talking about. I work in the embeded world, you can't go passing all these varriables around, it will kill the stack.
Anyway....

Use them in the page if that's what you want. You can argue whether or not it's good coding practice later. I use page-wide varriables all the time.
May 23 '07 #7
sraghu
5
after declaring global variable in <appSettings>. How do you use it in different page.
Jun 13 '07 #8
Frinavale
9,735 Expert Mod 8TB
after declaring global variable in <appSettings>. How do you use it in different page.
System.Configuration.ConfigurationManager.AppSetti ngs("nameOfVariableInAppSettings")

Cheers!

-Frinny
Jun 13 '07 #9
i found a very useful link on this issue..have a look at this..this might be useful for you:
how-to-create-global-functions-in-asp-net-4-0
Jun 12 '12 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: NotGiven | last post by:
I have an file I call using: require_once() In this file I have variables I'd like to use in the calling page and functions called by that page. How can I do this? example:
3
by: Charles Hartman | last post by:
I know the answer to this is going to be "It depends . . .", but I want to get my mind right. In Fowler's *Refactoring* I read: "Older languages carried an overhead in subroutine calls, which...
10
by: Andy Fish | last post by:
Hi, can anybody put forward a sensible argument javascript's behaviour of creating a new global variable whenever I assign to a previously undeclared variable. I can't beleive this is just for...
6
by: David T. Ashley | last post by:
Hi, In my project, I typically declare and define variables in the .H file, i.e. DECMOD_MAIN UINT8 can_message_201_status_global #ifdef MODULE_MAIN = HAS_NEVER_BEEN_RECEIVED #endif ;
6
by: rick | last post by:
Noob problem. I prefer to keep all my scripts in an external '.js' file. I am currently loading the external '.js' file from the header. Problem is I would like to declare a global variable in the...
33
by: MLH | last post by:
I've read some posts indicating that having tons of GV's in an Access app is a bad idea. Personally, I love GVs and I use them (possibly abuse them) all the time for everything imaginable - have...
41
by: Miguel Dias Moura | last post by:
Hello, I am working on an ASP.NET / VB page and I created a variable "query": Sub Page_Load(sender As Object, e As System.EventArgs) Dim query as String = String.Empty ... query =...
8
by: SM | last post by:
I've always wonder if there is diference when declaring and initializing a varible inside/outside a loop. What's a better practice? Declaring and initializing variables inside a loop routine,...
5
by: dancer | last post by:
Using ASP.net 1.1 and VB Is it possible to declare variables in their own subroutine? I had my DIM statements within a sub that worked just fine. I wanted to be able to use them in another...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.