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;
        }
        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在很多未连接的课程中使用,我该如何正确地做到这一点?

感谢帮助。

4

1 回答 1

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); }
}
于 2016-11-18T08:27:15.717 回答