0

I have the following method which handles DoWork of my BackgroundWorker

Public Class BackgroundRunner
   Private Sub BgTask(ByVal sender As Object, ByVal e As DoWorkEventArgs)
       AnotherForm.BgTask(e.Argument)
   End Sub
End Class

Public Class AnotherForm

   Public StartDate As Date

   Public Function BgTask(ByVal e As Object)
       'When I access StartDate here
       'when called via backgroundWorker, I get the default value of 12:00:00 AM
       'When called from another method in the same form, I get its original value
   End Function
End Class

How do I access the variable when called via a background worker?

Raj
  • 22,346
  • 14
  • 99
  • 142
  • Do you have a property called `AnotherForm` in your calling class? Your question is very unclear at the moment. – Jon Skeet Jul 28 '13 at 15:03
  • @JonSkeet No. AnotherForm is not a property of the calling class. It is the name of some other form. – Raj Jul 28 '13 at 15:07
  • 1
    So which instance are you *expecting* it to use? You're telling `BackgroundWorker` to invoke an *instance* method, which means it needs to be on an instance of the form - which instance? This is almost certainly the problem. I suspect VB is implicitly creating a new instance of the form, which isn't what you want. (C# wouldn't let you do this.) – Jon Skeet Jul 28 '13 at 15:18
  • @JonSkeet Thanks! Now I moved my variable to a separate module (so can be used as Global Variables). Now I get its original value when called directly as well as via background worker. – Raj Jul 28 '13 at 16:03
  • I don't know what options you use, but above code produces an error "Reference to a non-shared member requires an object reference." (and also a warning of "no return type specified" for the function BgTask). – Styxxy Jul 28 '13 at 22:19
  • @Styxxy No I don't get that error. But while debugging, if I watch `AnotherForm.StartDate` while not in AnotherForm, I see your error in the Watch window. – Raj Jul 30 '13 at 14:40
  • If you copy above code into a console application it doesn't work (I wasn't planning on creating a WinForm app.) Anyway, I see that it is marked as duplicate. – Styxxy Jul 30 '13 at 15:18

0 Answers0