Good Way to declare global variables in C# NET

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • johnlim20088
    New Member
    • Jan 2007
    • 74

    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)?
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    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.
    Code:
    public static class GlobalVars
    {
        private static AppSettingsReader ap = new AppSettingsReader();
        public static bool isDebug
        {
            get
            {
                return System.Diagnostics.Debugger.IsAttached;
            }
        }
    
       public static string PDFBaseDir
       {
          get
          {
             return ap.GetValue("PDFBaseDir", typeof(string)).ToString();
          }
       }

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Originally posted by johnlim20088
      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

      Comment

      • Motoma
        Recognized Expert Specialist
        • Jan 2007
        • 3236

        #4
        Originally posted by johnlim20088
        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.
        Last edited by Motoma; May 22 '07, 02:21 PM. Reason: Wrong link :P

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          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]

          Comment

          • johnlim20088
            New Member
            • Jan 2007
            • 74

            #6
            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


            Originally posted by Frinavale
            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

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              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.

              Comment

              • sraghu
                New Member
                • Jun 2007
                • 5

                #8
                after declaring global variable in <appSettings> . How do you use it in different page.

                Comment

                • Frinavale
                  Recognized Expert Expert
                  • Oct 2006
                  • 9749

                  #9
                  Originally posted by sraghu
                  after declaring global variable in <appSettings> . How do you use it in different page.
                  System.Configur ation.Configura tionManager.App Settings("nameO fVariableInAppS ettings")

                  Cheers!

                  -Frinny
                  Last edited by Frinavale; Jun 12 '12, 12:56 PM.

                  Comment

                  • coolansh63
                    New Member
                    • Jun 2012
                    • 1

                    #10
                    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

                    Comment

                    Working...