0

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

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

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

1 回答 1

0

您在 MyBackGroundWorkerObject 中定义了嵌套类,但在 MyBackGroundWorkerObject 类中没有声明 TimeZone 类型的变量或 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 访问它

于 2016-11-21T20:28:54.903 回答