我正在尝试使用嵌套类以将两个类传递到一个参数中,以便我可以将其发送到后台工作者。到目前为止,我设法将单个参数传达到了一个背景工作者中,但是我还没有使用嵌套的班级来完成,最终我将所需的类或我所需的班级传递到同一参数中。到目前为止,这里是我正在使用的一些代码:
这是我试图使用的嵌套类:
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);
,这是我完全困惑的部分,我希望能够从窗口的表单中从窗口的表单中从其检索一些输入的数据时使用两者。我不确定如何使这两个班级在联想中工作,在那里我可以同时将它们都发送给背景工作者:
MyBackGroundWorkerObject myBackGroundWorker1 = new MyBackGroundWorkerObject();
object obj = ddlAccounts.SelectedItem;
MyBackGroundWorkerObject.AccountName acct = obj as MyBackGroundWorkerObject.AccountName;
backgroundWorker1.RunWorkerAsync(acct);
您在mybackgroundworkerObject中定义了嵌套类,但是类型时区也没有类型type accountname的变量在mybackgroundworkerobject类中声明。
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);
在后台工人内部,将其投射到MybackgroundWorkerObject,并再次通过.TheTimeZone和.TheAccountName访问