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. General Programming
  3. C#
  4. Application Global Properties

Application Global Properties

Scheduled Pinned Locked Moved C#
help
11 Posts 4 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.
  • J Offline
    J Offline
    John Baird
    wrote on last edited by
    #1

    Hello: Is there a way to set application properties that are global and accessible from all points in the program. I have tried public static classes, constants, properties on the main form, properties in the app object, but it always tells me that the "prop" is not in scope. How do you guys handle this kind of problem for setting application wide defaults. It was easy in vfp, but i'm pulling my hair out here (what's left of it anyway). Thanks.

    C 1 Reply Last reply
    0
    • J John Baird

      Hello: Is there a way to set application properties that are global and accessible from all points in the program. I have tried public static classes, constants, properties on the main form, properties in the app object, but it always tells me that the "prop" is not in scope. How do you guys handle this kind of problem for setting application wide defaults. It was easy in vfp, but i'm pulling my hair out here (what's left of it anyway). Thanks.

      C Offline
      C Offline
      CWIZO
      wrote on last edited by
      #2

      Having one class with static fields is good. Just make shure that all class'es are in the same namespace. Q:What does the derived class in C# tell to it's parent? A:All your base are belong to us!

      J 1 Reply Last reply
      0
      • C CWIZO

        Having one class with static fields is good. Just make shure that all class'es are in the same namespace. Q:What does the derived class in C# tell to it's parent? A:All your base are belong to us!

        J Offline
        J Offline
        John Baird
        wrote on last edited by
        #3

        The classes are in different namespaces. I thought adding a reference to the individual namespaces would allow me access to the class from anywhere. Is that not true?

        C 1 Reply Last reply
        0
        • J John Baird

          The classes are in different namespaces. I thought adding a reference to the individual namespaces would allow me access to the class from anywhere. Is that not true?

          C Offline
          C Offline
          CWIZO
          wrote on last edited by
          #4

          By adding refrence you mean: using YourNameSpace; ? the using statment is only aplied to one file if it is placed outside the namespace {} block! Q:What does the derived class in C# tell to it's parent? A:All your base are belong to us!

          J 1 Reply Last reply
          0
          • C CWIZO

            By adding refrence you mean: using YourNameSpace; ? the using statment is only aplied to one file if it is placed outside the namespace {} block! Q:What does the derived class in C# tell to it's parent? A:All your base are belong to us!

            J Offline
            J Offline
            John Baird
            wrote on last edited by
            #5

            Thanks for replying, but I'm still lost. I'm trying to load the values into a static structure now. The values are loaded, but when I try to access them from the watchwindow, I get the "out of score" error. Pardon me, but here is the code. Can you tell me what's wrong?

            using System;
            using System;
            using AMS.Profile;
            
            
            namespace MySpace
            {
            	/// 
            	/// Summary description for AppStartUp.
            	/// 
            	public class AppStartUp
            	{
            		public AppStartUp()
            		{
            			//
            			// TODO: Add constructor logic here
            			//
            		}
            
            		public struct AppDefaults
            		{
            			public static string server = "";
            			public static string dataBase = "";
            			
            			public static string adminPath = "c:\vsdevelop";
            			public static string sqlConn = "";		
            		}
            
            		public static void SetAppDefaults()
            		{
            			Registry profile = new Registry();
            			AppDefaults.server = profile.GetValue("DBSettings","DBServer","");
            			AppDefaults.dataBase = profile.GetValue("DBSettings","DBName","");
            			
            			AppDefaults.adminPath = "c:\vsdevelop";
            			AppDefaults.sqlConn = "SERVER=" + AppDefaults.server.ToUpper().Trim() + ";UID=uid; PWD=pwd;DATABASE=database";
            		}
            		
            		public static void LoadProgramSettings()
            		{
            			SetAppDefaults();
            			
            		}
            	}
            }
            
            C H 2 Replies Last reply
            0
            • J John Baird

              Thanks for replying, but I'm still lost. I'm trying to load the values into a static structure now. The values are loaded, but when I try to access them from the watchwindow, I get the "out of score" error. Pardon me, but here is the code. Can you tell me what's wrong?

              using System;
              using System;
              using AMS.Profile;
              
              
              namespace MySpace
              {
              	/// 
              	/// Summary description for AppStartUp.
              	/// 
              	public class AppStartUp
              	{
              		public AppStartUp()
              		{
              			//
              			// TODO: Add constructor logic here
              			//
              		}
              
              		public struct AppDefaults
              		{
              			public static string server = "";
              			public static string dataBase = "";
              			
              			public static string adminPath = "c:\vsdevelop";
              			public static string sqlConn = "";		
              		}
              
              		public static void SetAppDefaults()
              		{
              			Registry profile = new Registry();
              			AppDefaults.server = profile.GetValue("DBSettings","DBServer","");
              			AppDefaults.dataBase = profile.GetValue("DBSettings","DBName","");
              			
              			AppDefaults.adminPath = "c:\vsdevelop";
              			AppDefaults.sqlConn = "SERVER=" + AppDefaults.server.ToUpper().Trim() + ";UID=uid; PWD=pwd;DATABASE=database";
              		}
              		
              		public static void LoadProgramSettings()
              		{
              			SetAppDefaults();
              			
              		}
              	}
              }
              
              C Offline
              C Offline
              CWIZO
              wrote on last edited by
              #6

              You are trying to set a value to a field that is inside the structure, onyl specifying the class ... And even so I think you must declare the structure as static too ... but I really don't know why you are using a structure in the above code (dump it)... Q:What does the derived class in C# tell to it's parent? A:All your base are belong to us!

              J 1 Reply Last reply
              0
              • C CWIZO

                You are trying to set a value to a field that is inside the structure, onyl specifying the class ... And even so I think you must declare the structure as static too ... but I really don't know why you are using a structure in the above code (dump it)... Q:What does the derived class in C# tell to it's parent? A:All your base are belong to us!

                J Offline
                J Offline
                John Baird
                wrote on last edited by
                #7

                Thanks for your help. I've finally been able to get it to work. Dump it in favor of what? classes. There has been some discussion on speed using structures for static storage. It seems that most favor the structure. You reasons?

                C 1 Reply Last reply
                0
                • J John Baird

                  Thanks for your help. I've finally been able to get it to work. Dump it in favor of what? classes. There has been some discussion on speed using structures for static storage. It seems that most favor the structure. You reasons?

                  C Offline
                  C Offline
                  CWIZO
                  wrote on last edited by
                  #8

                  It has no sense in using a structure like that. There is no need for you to "group" fields inside a structure, becouse you already have them inside your class. Maybe you should read some article that covers the OO aproach to desigening programms :) Q:What does the derived class in C# tell to it's parent? A:All your base are belong to us!

                  1 Reply Last reply
                  0
                  • J John Baird

                    Thanks for replying, but I'm still lost. I'm trying to load the values into a static structure now. The values are loaded, but when I try to access them from the watchwindow, I get the "out of score" error. Pardon me, but here is the code. Can you tell me what's wrong?

                    using System;
                    using System;
                    using AMS.Profile;
                    
                    
                    namespace MySpace
                    {
                    	/// 
                    	/// Summary description for AppStartUp.
                    	/// 
                    	public class AppStartUp
                    	{
                    		public AppStartUp()
                    		{
                    			//
                    			// TODO: Add constructor logic here
                    			//
                    		}
                    
                    		public struct AppDefaults
                    		{
                    			public static string server = "";
                    			public static string dataBase = "";
                    			
                    			public static string adminPath = "c:\vsdevelop";
                    			public static string sqlConn = "";		
                    		}
                    
                    		public static void SetAppDefaults()
                    		{
                    			Registry profile = new Registry();
                    			AppDefaults.server = profile.GetValue("DBSettings","DBServer","");
                    			AppDefaults.dataBase = profile.GetValue("DBSettings","DBName","");
                    			
                    			AppDefaults.adminPath = "c:\vsdevelop";
                    			AppDefaults.sqlConn = "SERVER=" + AppDefaults.server.ToUpper().Trim() + ";UID=uid; PWD=pwd;DATABASE=database";
                    		}
                    		
                    		public static void LoadProgramSettings()
                    		{
                    			SetAppDefaults();
                    			
                    		}
                    	}
                    }
                    
                    H Offline
                    H Offline
                    Heath Stewart
                    wrote on last edited by
                    #9

                    To add, you may want to consider caching the settings instead of reading them from the registry each time. If they can be changed throughout your application, then have additional methods on this class that can reset some flag so that the values would be read and cached again. Also, use a private constructor so that callers can't instantiate this class since that would serve no purpose. In .NET 2.0, you don't need to do this if you use static classes (prefix your class declaration with static). This will be out early next year, just FYI.

                    Microsoft MVP, Visual C# My Articles

                    A 1 Reply Last reply
                    0
                    • H Heath Stewart

                      To add, you may want to consider caching the settings instead of reading them from the registry each time. If they can be changed throughout your application, then have additional methods on this class that can reset some flag so that the values would be read and cached again. Also, use a private constructor so that callers can't instantiate this class since that would serve no purpose. In .NET 2.0, you don't need to do this if you use static classes (prefix your class declaration with static). This will be out early next year, just FYI.

                      Microsoft MVP, Visual C# My Articles

                      A Offline
                      A Offline
                      Anonymous
                      wrote on last edited by
                      #10

                      Hello: Thanks for responding. I'm loading the values into the global properties one time at application startup. In that instance, would caching the values give me any benefit?

                      H 1 Reply Last reply
                      0
                      • A Anonymous

                        Hello: Thanks for responding. I'm loading the values into the global properties one time at application startup. In that instance, would caching the values give me any benefit?

                        H Offline
                        H Offline
                        Heath Stewart
                        wrote on last edited by
                        #11

                        Well first, you need to expose them. You're simply setting them on a struct, which goes out of scope so they can never be read again. While I don't particularly find this approach to settings at all attractive from an OO standpoint, the least you can do is provide static properties on this class using the guidelines I outlined. What you're doing right now won't work at all because the struct simply goes out of scope like in the rest of your thread the other replier mentioned.

                        Microsoft MVP, Visual C# My Articles

                        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