c# twincat 将参数传递给 class 构造函数

c# twincat passing argument to class constructor

我不知道什么是最好的方法。

如果我需要将 class 传递给 class 构造函数,为什么我应该在 class.

中使用变量

示例:

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); }
    }

我想在很多 class 未连接的情况下使用 _adsServer,我该如何正确执行?

感谢您的帮助。

好的,这就是我现在做的。

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); }
}