I'm trying to use a nested class to get two classes to pass into a single argument so I can send it to a backgroundworker. Thus far, I've managed to pass single arguments into a backgroundworker but I'm yet to do it with a nested class where I end up passing both or my desired classes into the same argument. So far here is some of the code I'm using:
This is the Nested Class I'm attempting to use:
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;
}
}
}
Here's an example of one of the classes in action:
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);
And here's the part where I'm utterly confused, I'd like to be able to use both of these when calling from the Window's Form from where it's retrieving some of the entered data. I'm not sure on how to get both of these classes to work in conjuction where I can send them both at the same time to the backgroundworker:
MyBackGroundWorkerObject myBackGroundWorker1 = new MyBackGroundWorkerObject();
object obj = ddlAccounts.SelectedItem;
MyBackGroundWorkerObject.AccountName acct = obj as MyBackGroundWorkerObject.AccountName;
backgroundWorker1.RunWorkerAsync(acct);
You defined the nested classes inside MyBackGroundWorkerObject but there is no variable of type TimeZone nor of type AccountName declared inside the MyBackGroundWorkerObject class.
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;}
}
Now you can set your instances via the TheTimeZone and the TheAccountName members respectively and access them when you pass the 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);
Inside the background worker doWork cast it to MyBackGroundWorkerObject and access it via .TheTimeZone and .TheAccountName again