title: Access background worker from a different class
body: <p>I'm separating out logic from one class to another in my Windows Form application. When all in one class, it works fine but I am moving the logic to it's own respective classes now. Having some issues getting at the backgroundWorker from a different class.</p>
  
  <p>I have in class A</p>
  
  <pre><code>public partial class ClassA : Form
      BackgroundWorker essentialBgWorker = new BackgroundWorker();
  
      public ClassA()
          {
              InitializeComponent();
              //essentialBgWorker
              essentialBgWorker.DoWork += new DoWorkEventHandler(essentialBgWorker_DoWork);
              essentialBgWorker.ProgressChanged += new ProgressChangedEventHandler(essentialBgWorker_ProgressChanged);
              essentialBgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(essentialBgWorker_RunWorkerCompleted);
              essentialBgWorker.WorkerReportsProgress = true;
          }
  </code></pre>
  
  <p>That is a form that has a button which when clicked, copies files to another directory.</p>
  
  <pre><code>private void copyButton_Click(object sender, EventArgs e)
  {
      clickedButton = ((Button)sender).Name.ToString();
      itemsChanged = ((Button)sender).Text.ToString();
      essentialBgWorker.RunWorkerAsync();
  }
  </code></pre>
  
  <p>This runs the background worker:</p>
  
  <pre><code>public void essentialBgWorker_DoWork(object sender, DoWorkEventArgs e)
  {
      string buttonSender = clickedButton; //copyButton, deleteButton, etc.
      switch (buttonSender)
      {
          case "copyButton":
              //this is pseudocode - the important part being that this is where I would call the method from the other class
              ClassB.copyDocuments();
              break;
          case "deleteButton":
              //this is pseudocode - the important part being that this is where I would call the method from the other class
              ClassB.deleteDocuments();
              break;
          default:
              essentialBgWorker.CancelAsync();
              break;
      }
  }
  </code></pre>
  
  <p>In my other class (<code>ClassB</code>) for this example I have the method that is called when the button is clicked (should be handling the logic).</p>
  
  <pre><code>public void copyDocuments()
  {
      ClassA classA = new ClassA();
      //
      //the logic that handles copying the files
      //gets the filecount!
  
      //Report to the background worker
      int totalFileCount = fileCount;
      int total = totalFileCount; //total things being transferred
      for (int i = 0; i &lt;= total; i++) //report those numbers
      {
          System.Threading.Thread.Sleep(100);
          int percents = (i * 100) / total;
          classA.essentialBgWorker.ReportProgress(percents, i);
  
          //2 arguments:
          //1. procenteges (from 0 t0 100) - i do a calcumation 
          //2. some current value!
      }
      //Do the copying here
  }
  </code></pre>
  
  <p>I keep having an issue with this line:</p>
  
  <p><code>classA.essentialBgWorker.ReportProgress(percents, i);</code></p>
  
  <p>This line worked when it was <em>in</em> ClassA - but once bringing it into ClassB gives an error:</p>
  
  <p><code>ClassA.essentialBgWorker' is inaccessible due to it's protection level</code></p>
  
  <p>Just looking for some assistance on the proper way to get it to fire from the other class.</p>