469,281 Members | 2,450 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,281 developers. It's quick & easy.

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 29373
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

Post your reply

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

Similar topics

3 posts views Thread by Charles Hartman | last post: by
6 posts views Thread by rick | last post: by
41 posts views Thread by Miguel Dias Moura | last post: by
5 posts views Thread by dancer | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.