0

I've read alot of questions here about accessing labels and UI elements from static methods, but I haven't really found any solution I can apply to my project.

In the main window of my application, I have a method that calls another method, in another class. That method syncronizes some XML readings and stores them to a local file.

This is how I want it to behave:

I call this method (Located in my MainWindow source file) from anywhere inside the namespace:

public static async void UpdateAllLocalFeedsAsync()
{
   lblStatusBar.Content = "Working-working etc...";

   var result = await ThisFunctionsSyncronizesXmlWithLocalFiles(Parameter);

   lblStatusBar.Content = "The job has been done.";

Print some info that tells the user what is about to happen, do the stuff, and tell the user it's been done.

At this moment, everything works smooth as silk, as long as I exclude the lblStatusBar-rows. But as expected, I get the following error when I try to change the content in the label:

Cannot access non-static field 'lblStatusBar' in static context

If I try to make the methods non-static, the async/await abilities seems to fail, requiring them to be static.

I did however find this answer, here on SO...

static void SetTextboxTextSafe(int result) 
{
   label1.Text = result.ToString();
}

... which I unfortunately did not understand how to apply on a label. My apologies if I'm not being clear enough, but I hope you understand my question.

Community
  • 1
  • 1
  • 4
    How much do you understand about what `static` means? I would suggest you master that kind of core aspect of the language before you try asynchrony. – Jon Skeet Nov 04 '14 at 19:02
  • methods don't need to be `static` to be `async`. – Servy Nov 04 '14 at 19:03
  • A `static` member does not belong to any class instance, it exists since the program started, so you wouldn't know what `lblStatusBar` means. The application could have instantiated the containing class zero or multiple times. Which one would you use? That's why you can't access instance members from static members. – Andrew Nov 04 '14 at 19:05
  • @JonSkeet It is like a class method instead of an instance method? I am trying to figure this out, since it is important for the rest of the project to work because of the time needed to complete the method call. I'm really trying to understand everything that is happening, and I guess there must be a way to sort this out. The only difference is the output I really want to happen. I've realized that making a label static is not a good option or even possible. So if I am doing this completely wrong, how should I approach the problem? – swaglord mcmuffin' Nov 04 '14 at 19:10
  • Yes, it applies to the class rather than any specific instance of the class. So, suppose there were multiple instances of your form... which of those labels would you expect to be modified? – Jon Skeet Nov 04 '14 at 19:12
  • @Servy, but in order to be able to call the function called "UpdateAllLocalFeedAsync()" from different classes in the same namespace, I've only managed it to work by switching to static, but I might just have missed something else? Sorry for being kinda dumb :) – swaglord mcmuffin' Nov 04 '14 at 19:18
  • @LinusWaerner To call the method *without an instance of the class* it needs to be static. If you have an instance of the class then the method doesn't need to be static. None of this has anything to do with asynchrony though. – Servy Nov 04 '14 at 19:21
  • @JonSkeet, alright. I understand what you're saying, and that sure sounds like an explanation I haven't thought of myself. But I can't seem to find a solution to this. I really need to do some homework here. All I really want is to call a function in my main window from another class instance, and print some info, but with this in mind, it sounds like i really need to approach the task from another direction. – swaglord mcmuffin' Nov 04 '14 at 19:24
  • Well you should think about which piece of code knows about the form containing the label you want to update... – Jon Skeet Nov 04 '14 at 19:27
  • I solved it by creating an instance of the class containing "ThisFunctionsSyncronizesXmlWithLocalFiles()" in MainWindow, and after that, I could easily remove all static declarations. Thank you for helping me help myself! In a few years I might be able to post a question that actually is a good one. Thanks again! – swaglord mcmuffin' Nov 04 '14 at 19:44

0 Answers0