I really never understood the difference of why one would use a static readonly property vs. a const, until now.  I very much dislike having string values hardcoded and would rather make them strongly typed to use throughout the application.  This works great, unless those constants can’t be determined until runtime.  So here is my solution, make them public static readonly members that read config file settings to help get the value at runtime.

Example:

 

    public static class MyPages

    {

        /// <summary>

        /// Login Page Url

        /// </summary>

        public static readonly string Login = GetUrl(“/Login/Login.aspx”);

 

 

        private static string GetUrl(string page)

        {

            return Settings.Default.WebRoot + page;

        }

 

Notice that I have a static class with a private method that goes out to the settings and updates the url with a value from the settings class.  This allows the URL to be configurable at runtime, for instance, the url code become relative, rootlevel, or other means.

 

Your thoughts?

 

Technorati Tags: ,
Advertisements