Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I use concatenation on a public static string

FYI I spent about 30 minutes searching for the answer. If I missed it on stackoverflow, i'm sorry. This seems like an easy answer, however none of my colleagues know either.

I have am working with an existing library. I'm trying to maintain integration with a current system, while adding the ability to change some hard-coded values. I've refactored the code to utilize the ConfigurationManager so I can use parametrized web deployment.

My question is this.. Why, when I access Constants.CourseMillRegisterURL, I only get back part of the variable? The part that I get back is the piece read from web.config. I expect to get a complete URL containing both variables concat'd but I only get my web.config value "userlogin.jsp".

I've tried also coding it so that the values are concat'd in the privates, but it doesn't work that way either. I really want to stay with static because the whole library refers to this class using code like

string theUrl = Constants.CoursMillUrl + Constants.CourseMillRegisterUrl

Each variable returns the following:

  • Constants.CourseMillUrl = "http://www.valuefromwebconfig.com/cm6/cm0670"
  • Constants.CourseMillRegisterUrl = "valuefromwebconfig.jsp"
  • Constants.CourseMillLoginUrl = "othervaluefromwebconfig.jsp"

Why are my values not

  • Constants.CourseMillUrl = "http://www.valuefromwebconfig.com/cm6/cm0670"
  • Constants.CourseMillRegisterUrl = ""http://www.valuefromwebconfig.com/cm6/cm0670/valuefromwebconfig.jsp"
  • Constants.CourseMillLoginUrl = "http://www.valuefromwebconfig.com/cm6/cm0670/othervaluefromwebconfig.jsp"

My code is below.

namespace STTI.CourseMill.Library
{
    #region

    using System.Configuration;

    #endregion

    public static class Constants
    {
        // prod 

        #region Static Fields

        public static string CourseMillRegisterURL = CourseMillURL + courseMillRegisterURL;

        public static string CourseMillURL = courseMillURL;

        public static string CourseMillUserLoginURL = CourseMillURL + courseMillUserLoginURL;

        #endregion

        #region Properties

        private static string courseMillRegisterURL
        {
            get
            {
                string output = ConfigurationManager.AppSettings["CourseMillRegisterUrl"];
                if (output == null)
                {
                    output = "sttilogin.jsp?d=t";
                }

                return output;
            }
        }

        private static string courseMillURL
        {
            get
            {
                string output = ConfigurationManager.AppSettings["CourseMillURL"];
                if (output == null)
                {
                    output = "http://hardcodedvalue/cm6/cm0670";
                }

                if (!output.EndsWith("/"))
                {
                    output += "/";
                }

                return output;
            }
        }

        private static string courseMillUserLoginURL
        {
            get
            {
                string output = ConfigurationManager.AppSettings["CourseMillLoginUrl"];
                if (output == null)
                {
                    output = "sttilogin.jsp?d=t";
                }

                return output;
            }
        }

        #endregion
    }
}
like image 879
CarComp Avatar asked Jan 11 '23 16:01

CarComp


1 Answers

Static strings are initialised in the order they appear in the file.

courseMillRegisterURL is initialised after CourseMillRegisterURL, for example.

That's why your strings are incomplete.

like image 196
Bathsheba Avatar answered Jan 17 '23 20:01

Bathsheba