2015-01-28 3 views
0

내가 클래스에 따라 목록은 아래의 선언을 찾을 수 없습니다 같은 클래스에서이 (common.cs) :방법은 내 common.cs 클래스에서

#region PublicMethods 

public List<edbService> populateEDBService(string xmlDataFile) 
{ 
try 
{ 
XElement x = XElement.Load(global::EvryCardManagement.Properties.Settings.Default.DataPath + xmlDataFile); 

// Get global settings 
IEnumerable<XElement> services = from el in x.Descendants("Service") 
     select el; 
if (services != null) 
{ 
    edb_service = new List<edbService>(); 
    foreach (XElement srv in services) 
    { 
    edbService edbSrv = new edbService(); 

    edbSrv.ServiceID = srv.Element("ServiceID").Value; 
    edbSrv.ServiceName = srv.Element("ServiceName").Value; 
    edbSrv.ServiceDescr = srv.Element("ServiceDescr").Value; 
    edbSrv.ServiceInterval = srv.Element("ServiceInterval").Value; 
    edbSrv.ServiceStatus = srv.Element("ServiceStatus").Value; 
    edbSrv.ServiceUrl = srv.Element("ServiceUrl").Value; 

    foreach (XElement ServiceHeader in srv.Elements("ServiceHeader")) 
    { 
    edbSrv.SourceApplication = ServiceHeader.Element("SourceApplication").Value; 
    edbSrv.DestinationApplication = ServiceHeader.Element("DestinationApplication").Value; 
    edbSrv.Function = ServiceHeader.Element("Function").Value; 
    edbSrv.Version = ServiceHeader.Element("Version").Value; 

    foreach (XElement ClientContext in ServiceHeader.Elements("ClientContext")) 
    { 
    edbSrv.userid = ClientContext.Element("userid").Value; 
    edbSrv.credentials = ClientContext.Element("credentials").Value; 
    edbSrv.orgid = ClientContext.Element("orgid").Value; 
    edbSrv.orgunit = ClientContext.Element("orgunit").Value; 
    edbSrv.customerid = ClientContext.Element("customerid").Value; 
    edbSrv.channel = ClientContext.Element("channel").Value; 
     edbSrv.ip = ClientContext.Element("ip").Value; 
     } 
     } 

     edb_service.Add(edbSrv); 
     } 
    } 
    } 
    catch (Exception ex) 
    { 
    /* Write to log */ 
    Common.logBuilder("CustomerCreate : Form --> CustomerCreate <--", "Exception", Common.ActiveMQ, 
    ex.Message, "Exception"); 
    /* Send email to support */ 
    emailer.exceptionEmail(ex); 
    } 
    return edb_service; 
} 

하지만이 메서드에서 반환 목록을하려고 할 때 문제가 발견되지 않는, 내 호출 클래스입니다 - 내가 컴파일 오류가 발생하는 오브젝트 참조가 필요합니다.

Common.edbService edb_service = Common.populateEDBService("CardUpdate.xml");

을하고 난 다음 오류 얻을 :

나는 다음과 같이 호출하려고

An object reference is required for the non-static field, method, or property 'EvryCardManagement.Common.populateEDBService(string)'

내가 잘못하고있는 중이 야 무엇을?

나는 (내 양식에 배경 노동자에 의해 초기화 된 후 비동기 실행) 여러 클래스 당신이 필요로하는

+0

표시를 사용하여 다른 모든 클래스에서이 메서드를 호출 할 수 있습니다. –

+0

아마도'edbService'가 내부 클래스이고, 내부 클래스의 콜렉션을 리턴하려고 시도하고있는 중일 것입니다. 그러므로 그것이 내가 왜 그 문제라고 생각하는지. 'edbService' 클래스를 공용으로 만들어보십시오. 오류가 계속 발생하는지 확인하기 만하면됩니다. – AssaultingCuccos

+0

더 많은 세부 사항을 제공하기 위해 편집했습니다. 또한 공개되어 아직 오류가 발생합니다. –

답변

1

정적으로 메서드를 만들 수 있습니다.

public static List<edbService> populateEDBService(string xmlDataFile) 
{ 
    //Your code here 
    .... 
} 

지금 당신은 당신이 함수를 호출하려고하는 방법 common.populateEDBService();

+0

정적 인 메소드를 만들었고 시도했습니다. 'Common.edbService edb_service = Common.populateEDBService ("CardUpdate.xml");'이제 오류가 발생합니다. ** 'System.Collections.Generic.List '형식을 'EvryCardManagement.Common.edbService'형식으로 암시 적으로 변환 할 수 없습니다. * * –

+1

당신의 메소드가리스트를 반환하면, 단일 객체가 아닌리스트에 값을 할당해야합니다. –

+0

@SalvatoreSorbello :'List edbService = Common.populateEDBService ("CardUpdate.xml"); '감사합니다! –

1

에서 호출 할 수있는 일반적인 방법을하고 싶은 두 클래스 static를 만들거나를 만들 그것을 호출하는 객체.

class edbService { } 

public static void Main() { 
    //this is error 
    edbService.populateEDBService(""); 

    //this is correct 
    edbService s = new edbService(); 
    s.populateEDBService(""); 
} 

위 예제의 마지막 줄에는 컴파일러에서 필요한 객체 참조가 나와 있습니다. 여기서 s 변수는 객체 참조입니다.

1

XML에 누락 값이 있습니까? 값이 누락되면 .Value 속성이 작동하지 않습니다. 따라서 ServiceID가 누락되면 srv.Element("ServiceID").Value;이 오류를 발생시킵니다. 예를 들어, 대신 빈 문자열을 반환 할 수 있습니다. (string)srv.Element("ServiceID");

+0

일부는 누락 될 수 있지만 처리되었습니다. 제안 사항 덕분에 –

관련 문제