2016-11-15 2 views
0

가장 좋은 방법은 무엇일까요?# twincat 클래스 생성자에 인수 전달

클래스를 클래스 생성자에 전달해야하는 이유는 클래스 내에서 변수를 사용해야하는 이유입니다.

예 :

using Beckhoff.App.Ads.Core.Plc; 

Class test() 
{ 
    private static IBAAdsServer AdsServer; 
    private static IBAAdsCncClient _cncClient; 

    public test(IBAAdsServer _adsServer) //constructor 
    { 
     try 
     { 
      AdsServer = _adsServer; 
      _cncClient = AdsServer.GetAdsClient<IBAAdsCncClient>("CNC"); 
      _cncClient.Synchronize = true; 
     } 
     catch (Exception Except) 
     { MessageBox.Show("Error ! " + Except.Message); } 
    } 

이유는 수행 할 수 없습니다

using Beckhoff.App.Ads.Core.Plc; 

Class test() 
{ 
    private static IBAAdsCncClient _cncClient; 

    public test(IBAAdsServer _adsServer) //constructor 
    { 
     try 
     { 
      _cncClient = _adsServer.GetAdsClient<IBAAdsCncClient>("CNC"); 
      _cncClient.Synchronize = true; 
     } 
     catch (Exception Except) 
     { MessageBox.Show("Error ! " + Except.Message); } 
    } 

는 내가 제대로 것을 어떻게 할 수, 연결되지 않은 클래스의 많이 _adsServer을 사용 하시겠습니까?

도움 주셔서 감사합니다.

+0

그렇게 할 수 있습니다. 생성자가 완성되면 참조를 잃을 것입니다. 왜 그럴 수 없다고하니? 또한, 왜 GetAdsClient <...>을 클래스 외부로 가져와 클래스로 푸시하는 것이 좋을까요? – bixarrio

+0

네, 이렇게하는 것이 다른 방법이었습니다. 실제로이 방법을 사용합니다. 그러나이 방법을 사용하는 것이 더 좋은 방법인지 또 다른 더 좋은 방법이 있는지 궁금합니다. 감사 – Jablonovo

답변

0

좋아, 이제 여기처럼됩니다.

using Beckhoff.App.Ads.Core.Plc; 

Class test() 
{ 
private static IBAAdsServer AdsServer; 
private static IBAAdsCncClient _cncClient; 

public test(IBAAdsServer _adsServer)     //constructor 
{ 
    try 
    { 
     AdsServer = _adsServer; 
     _cncClient = AdsServer.GetAdsClient<IBAAdsCncClient>("CNC"); 
     _cncClient.Synchronize = true; 
     Classtocall newClass = ClasstoCall(_cncClient);//Passing the argument directly 
} 
catch (Exception Except) 
    { MessageBox.Show("Error ! " + Except.Message); } 
} 
관련 문제