Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. global variables

global variables

Scheduled Pinned Locked Moved ASP.NET
csharpquestion
17 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S sulabh2020

    i under stand ur problem , the best way to use the variable u need on all most all the forms is: make static varible in on class and use where ever u need to want to use it value or set its value as uer requirement. Note define that as public in class.

    Hello Forum Always be in touch to help about the topic ASP.NET

    A Offline
    A Offline
    Archana New to Dotnet
    wrote on last edited by
    #8

    any samples pls...

    C 1 Reply Last reply
    0
    • A Archana New to Dotnet

      any samples pls...

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #9

      public static class MyGlobalVariables
      {
      public static int someVariable;
      }

      NOTE: This can be an indication of poor design - You should not be thinking in terms of global variables. What you need to do is to think of what the purpose of the variable is and find out which class it should belong to. Put it there and have the class control access to it via a property.


      Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... "I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless." My website

      A N 2 Replies Last reply
      0
      • C Colin Angus Mackay

        public static class MyGlobalVariables
        {
        public static int someVariable;
        }

        NOTE: This can be an indication of poor design - You should not be thinking in terms of global variables. What you need to do is to think of what the purpose of the variable is and find out which class it should belong to. Put it there and have the class control access to it via a property.


        Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... "I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless." My website

        A Offline
        A Offline
        Archana New to Dotnet
        wrote on last edited by
        #10

        thanks ..Im taking your sugesstion given in note

        1 Reply Last reply
        0
        • C Colin Angus Mackay

          public static class MyGlobalVariables
          {
          public static int someVariable;
          }

          NOTE: This can be an indication of poor design - You should not be thinking in terms of global variables. What you need to do is to think of what the purpose of the variable is and find out which class it should belong to. Put it there and have the class control access to it via a property.


          Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... "I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless." My website

          N Offline
          N Offline
          N a v a n e e t h
          wrote on last edited by
          #11

          Great.

          Colin Angus Mackay wrote:

          have the class control access to it via a property.

          I heard writing properties inside class as a bad habit ? What do you say ?


          My Website | Ask smart questions

          P C 2 Replies Last reply
          0
          • A Archana New to Dotnet

            we have modules in vb.net windows application where we can declare our variable globally and use in N no.of forms.how in web application ? Thanks in advance.

            R Offline
            R Offline
            Rasal doss Solomon
            wrote on last edited by
            #12

            Variables can be declared as Global in the configuration file.

            1 Reply Last reply
            0
            • N N a v a n e e t h

              Great.

              Colin Angus Mackay wrote:

              have the class control access to it via a property.

              I heard writing properties inside class as a bad habit ? What do you say ?


              My Website | Ask smart questions

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #13

              Who told you that? Properties are not a bad habit at all - they allow you to hide the implementation of setting and retrieving values. To a certain extent, properties are just syntactic sugar hiding the fact that these are internally functions called set_... and get_....

              Deja View - the feeling that you've seen this post before.

              N 1 Reply Last reply
              0
              • P Pete OHanlon

                Who told you that? Properties are not a bad habit at all - they allow you to hide the implementation of setting and retrieving values. To a certain extent, properties are just syntactic sugar hiding the fact that these are internally functions called set_... and get_....

                Deja View - the feeling that you've seen this post before.

                N Offline
                N Offline
                N a v a n e e t h
                wrote on last edited by
                #14

                Thanks, But I read some where like exposing class variables through properties is a bad habit.. Any way I always used to do that, just seen Colin's code and got doubt again...


                My Website | Ask smart questions

                C 1 Reply Last reply
                0
                • N N a v a n e e t h

                  Great.

                  Colin Angus Mackay wrote:

                  have the class control access to it via a property.

                  I heard writing properties inside class as a bad habit ? What do you say ?


                  My Website | Ask smart questions

                  C Offline
                  C Offline
                  Colin Angus Mackay
                  wrote on last edited by
                  #15

                  N a v a n e e t h wrote:

                  I heard writing properties inside class as a bad habit ? What do you say ?

                  Good grief no. public fields (aka member variables) are a bad habit.


                  Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... "I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless." My website

                  1 Reply Last reply
                  0
                  • N N a v a n e e t h

                    Thanks, But I read some where like exposing class variables through properties is a bad habit.. Any way I always used to do that, just seen Colin's code and got doubt again...


                    My Website | Ask smart questions

                    C Offline
                    C Offline
                    Colin Angus Mackay
                    wrote on last edited by
                    #16

                    N a v a n e e t h wrote:

                    But I read some where like exposing class variables through properties is a bad habit

                    Actually, it is a very good habit. You may, presently, wish to allow direct access to the variables because the class doesn't care what outside entities set on the variable. But if you do that and then realise that in fact you need to fire off an event, or check the validity of the value then you can't do anything about it. Properties are methods in reality. A property called Address will actually compile to get_Address() and set_Address(string value). So, by using properties now, even although you may not strictly need them now, saves problems later when you make the field private. You can easily add code to the method (that backs the property) because the public interface has not changed. It isn't so easy moving from public field to public property because they are not the same thing. (Even although in C# the look like the same thing) Two blog entries of mine that might be of interest: * Why make fields in a class private, why not just make them public?[^] * The public fields debate again[^]


                    Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... "I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless." My website

                    N 1 Reply Last reply
                    0
                    • C Colin Angus Mackay

                      N a v a n e e t h wrote:

                      But I read some where like exposing class variables through properties is a bad habit

                      Actually, it is a very good habit. You may, presently, wish to allow direct access to the variables because the class doesn't care what outside entities set on the variable. But if you do that and then realise that in fact you need to fire off an event, or check the validity of the value then you can't do anything about it. Properties are methods in reality. A property called Address will actually compile to get_Address() and set_Address(string value). So, by using properties now, even although you may not strictly need them now, saves problems later when you make the field private. You can easily add code to the method (that backs the property) because the public interface has not changed. It isn't so easy moving from public field to public property because they are not the same thing. (Even although in C# the look like the same thing) Two blog entries of mine that might be of interest: * Why make fields in a class private, why not just make them public?[^] * The public fields debate again[^]


                      Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... "I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless." My website

                      N Offline
                      N Offline
                      N a v a n e e t h
                      wrote on last edited by
                      #17

                      Thanks that was excellent


                      My Website | Ask smart questions

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups