使用嵌套类将多个参数传递给后台工作程序

时间:2016-11-21 20:10:36

标签: c# backgroundworker inner-classes windowsformsintegration multiple-arguments

我正在尝试使用嵌套类将两个类传递给单个参数,以便将其发送给后台工作者。到目前为止,我已经设法将单个参数传递给后台工作者但我还没有用嵌套类来完成它,我最终将两个或我想要的类传递到同一个参数中。到目前为止,这里是我正在使用的一些代码:

这是我试图使用的嵌套类:

public class MyBackGroundWorkerObject
{
    public class TimeZone
    {
        public string Zone;

        public int difference;

        public override string ToString()
        {
            return Zone;
        }
    }

    public class AccountName
    {
        public string AccountSid;
        public string AuthToken;
        public string Name;
        public override string ToString()
        {
            return Name;
        }    
    }

}

以下是其中一个类的示例:

MyBackGroundWorkerObject.AccountName acct = new MyBackGroundWorkerObject.AccountName();
        //AccountName acct = new AccountName();
        acct.AccountSid = "abcd";
        acct.AuthToken = "xyz";
        acct.Name = "Potato";
        ddlAccounts.Items.Add(acct);

MyBackGroundWorkerObject.TimeZone region = new MyBackGroundWorkerObject.TimeZone();
        //TimeZone region = new TimeZone();
        region.Zone = "UTC";
        region.difference = 0;
        comboBox1.Items.Add(region);

这就是我完全混淆的部分,我希望能够从Window的表单中调用它来检索一些输入的数据时使用这两个部分。我不确定如何让这两个类同时工作,我可以同时将它们发送给后台工作者:

MyBackGroundWorkerObject myBackGroundWorker1 = new MyBackGroundWorkerObject();
object obj = ddlAccounts.SelectedItem;

            MyBackGroundWorkerObject.AccountName acct = obj as MyBackGroundWorkerObject.AccountName; 
backgroundWorker1.RunWorkerAsync(acct);

1 个答案:

答案 0 :(得分:0)

您在MyBackGroundWorkerObject中定义了嵌套类,但没有TimeZone类型的变量,也没有在MyBackGroundWorkerObject类中声明的AccountName类型的变量。

public class MyBackGroundWorkerObject
{
    public class TimeZone
    {
        public string Zone;

        public int difference;

        public override string ToString()
        {
            return Zone;
        }
    }

    public class AccountName
    {
        public string AccountSid;
        public string AuthToken;
        public string Name;
        public override string ToString()
        {
            return Name;
        }    
    }

    public TimeZone TheTimeZone {get; set;}
    public AccountName TheAccountName {get; set;}

}

现在,您可以分别通过TheTimeZone和TheAccountName成员设置实例,并在传递MyBackGroundWorkerObject时访问它们。

MyBackGroundWorkerObject myBackGroundWorker1 = new MyBackGroundWorkerObject();
MyBackGroundWorkerObject.AccountName acct = new MyBackGroundWorkerObject.AccountName();
    //AccountName acct = new AccountName();
    acct.AccountSid = "abcd";
    acct.AuthToken = "xyz";
    acct.Name = "Potato";
    ddlAccounts.Items.Add(acct);

MyBackGroundWorkerObject.TimeZone region = new MyBackGroundWorkerObject.TimeZone();
    //TimeZone region = new TimeZone();
    region.Zone = "UTC";
    region.difference = 0;
    comboBox1.Items.Add(region);

myBackGroundWorker1.TheTimeZone = region;
myBackGroundWorker1.TheAccountName = acct;

backgroundWorker1.RunWorkerAsync(myBackGroundWorker1);

在后台工作者doWork内部将其转换为MyBackGroundWorkerObject并通过.TheTimeZone和.TheAccountName再次访问它