0

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

1 Answers1

0

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

Frank J
  • 1,666
  • 13
  • 19
  • You're dead on, this pretty much what I need except for the part where I have most of the variables declared and where I send it to the backgroundworker are in two different areas (Form1() vs ButtonClick()) in order to give the user time to select the correct field. So how would I get this: backgroundWorker1.RunWorkerAsync(myBackGroundWorker1); to work in the button click? – Carlton Peterson Nov 21 '16 at 20:36
  • Well, that depends completely on your UI. If the user is selecting e.g. something from the different list boxes (the name and the time zone) you could on the SelectedItemChanged events respectively fill a created form level MyBackGroundWorkerObject's properties and then just pass that in on the button click event. Again there is not enough code here to answer that and it is a completely separate question altogether. – Frank J Nov 21 '16 at 20:50
  • Awesome, well thank you for your answer. I think you have sufficiently answered my question based on the resources I have given you :) – Carlton Peterson Nov 22 '16 at 16:19