Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / June 2007

Tip: Looking for answers? Try searching our database.

Static Proerties in C#

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Carlos Sosa Albert - 08 Jun 2007 22:59 GMT
I'm trying to work with STATIC properties in C#. I have some properties I
get from the registry, so I don't want to instance the class.
I have something like this, but I cannot get it to work. Whenever I try to
access these properties from other classes or project in the same solution
(referenced, of course), I cannot find them...

Would anybody be so kind to give me a hand?

Thanks a lot!!!
----

namespace MyLibrary
{
   public static class Config
   {
       private RegistryKey regMySoft =
Registry.LocalMachine.OpenSubKey("SOFTWARE\\MySoft");

       public static String MailServer
       {
           get
           {
               return regSoft.GetValue("mailServer").ToString();
           }
           set
           {
               regSoft.SetValue("mailServer", value);
           }
       }
   }
}
Morten Wennevik [C# MVP] - 09 Jun 2007 11:28 GMT
> I'm trying to work with STATIC properties in C#. I have some properties I
> get from the registry, so I don't want to instance the class.
[quoted text clipped - 27 lines]
>     }
> }

Nothing really wrong about your code (regMySoft needs to be static though), but you may want to check for the existance of the key, and if you need to create it, the registry key needs to have write permissions.

    public static class Config
    {
        private static RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\MySoft", true);
        private static RegistryKey softwareKey = Registry.LocalMachine.OpenSubKey("SOFTWARE", true);

        public static string MailServer
        {
            get
            {
                if (key == null)
                {
                    softwareKey.CreateSubKey("MySoft");
                    key = softwareKey.OpenSubKey("MySoft", true);
                    key.SetValue("mailServer", "Default value");
                }
                return key.GetValue("mailServer").ToString();
            }
            set { key.SetValue("mailServer", value); }
        }
    }

Signature

Happy coding!
Morten Wennevik [C# MVP]

DArnold - 09 Jun 2007 13:24 GMT
> I'm trying to work with STATIC properties in C#. I have some properties
> I get from the registry, so I don't want to instance the class.
[quoted text clipped - 27 lines]
>    }
> }

More something like this?

    public class Config
    {
        private static RegistryKey regMySoft =
                Registry.LocalMachine.OpenSubKey("SOFTWARE\\MySoft");

        public static String MailServer
        {
            get
            {
                return regMySoft.GetValue("mailServer").ToString();
            }
            set
            {
                regMySoft.SetValue("mailServer", value);
            }
        }
    }
 }

x = Config.MailServer;
Config.MailServer = x;
Carlos Sosa Albert - 12 Jun 2007 19:09 GMT
Thanks a lot Darnold, it worked just fine!
=)

>> I'm trying to work with STATIC properties in C#. I have some properties I
>> get from the registry, so I don't want to instance the class.
[quoted text clipped - 51 lines]
> x = Config.MailServer;
> Config.MailServer = x;

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.