2013-12-17 4 views
2

참고 답변을 검색하는 데 약 30 분이 소요되었습니다. 내가 stackoverflow에 그것을 놓친 경우, 미안 해요. 이것은 쉬운 대답 인 것처럼 보이지만 동료 중 누구도 알지 못합니다.공용 정적 문자열에 연결을 사용할 수없는 이유

저는 기존 라이브러리를 사용하고 있습니다. 현재 시스템과의 통합을 유지하면서 하드 코딩 된 값을 변경할 수있는 기능을 추가하려고합니다. 나는 매개 변수화 된 웹 전개를 사용할 수 있도록 ConfigurationManager를 활용하도록 코드를 리팩터링했다.

내 질문은 .. 왜 내가 Constants.CourseMillRegisterURL에 액세스 할 때 변수의 일부만 얻게 되나요? 내가 돌아 오는 부분은 web.config에서 읽은 부분입니다. concat'd 두 변수를 모두 포함하는 완전한 URL을 얻을 것으로 기대하지만 web.config 값 "userlogin.jsp"만받습니다.

나는 또한 값을 concate'd 그래서 그 방식으로 작동하지 않도록 코딩했는데 시도했다.

  • Constants.CourseMillUrl = "http://www.valuefromwebconfig.com/cm6/cm0670"
  • Constants.CourseMillRegisterUrl : 전체 라이브러리가 각 변수는 다음 반환

    string theUrl = Constants.CoursMillUrl + Constants.CourseMillRegisterUrl 
    

    과 같은 코드를 사용하여이 클래스를 참조하기 때문에 정말 정적과 함께 있고 싶어 = "valuefromwebconfig.jsp"

  • Constants.CourseMillLoginUrl = "othervaluefromwebconfig.jsp"

왜 값은하지

내 코드가 있습니다 이하.

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 
    } 
} 
+4

질문에 대한 답변이 아니지만 URL 결합을 위해 문자열 연결을 사용하지 마십시오. ['System.Uri'] (http://msdn.microsoft.com/en-us/library/system.uri (v = vs.110) .aspx) 클래스를 대신 사용하십시오. – Rik

+0

이것에 대해 살펴 보겠습니다. 보스는 '빨리 고쳐주세요'라고 말했습니다. – CarComp

+1

밧 세바의 대답은 틀리지 만,이 공중판은 덮어 쓸 수 있습니까? 그렇지 않은 경우 읽기 전용 속성으로 구현하는 것이 좋습니다. 그러면 순서가 중요하지 않게됩니다. –

답변

7

정적 문자열은 파일에 표시된 순서대로 초기화됩니다.

courseMillRegisterURL은 예를 들어 CourseMillRegisterURL 이후에 초기화됩니다.

문자열이 불완전한 이유는 그 때문입니다.

+0

이 테스트를 거쳤으며 사실 내 문제를 해결했습니다. 고맙습니다. – CarComp

관련 문제