0

In the following code I'm declaring application level static variable and then accessing it from MainWindow.xaml.cs. Should using static variable here be avoided and instead instantiate the App class in the MainWindow.xaml.cs code below first and use the variable there as var app = Application.Current as App; app.myVariable = "Some Value";. I've read at some places (such as here) that generally we should avoid using static variables.

App.xaml.cs:

public partial class App : Application
{
    private static string myVarialbe {get; set;}
    ...........
}

MainWindow.xaml.cs:

public partial class MainWindow : Window
{
  ..........
  private void btnUserUpdate_Click(object sender, RoutedEventArgs e)
  {
    App.myVariable = "Some value";
    ......
  }
}
nam
  • 21,967
  • 37
  • 158
  • 332
  • 1
    The post you are referencing [Using Static method and variables - Good vs Bad](https://stackoverflow.com/questions/7346496/using-static-method-and-variables-good-vs-bad) already has answer. You defined a property, not a filed. So, everything okay. You can add synchronization if needed. – Jackdaw Nov 26 '20 at 16:44
  • 1
    @Jackdaw Yes, in the answer to that linked post , when I read the sentence `it seems like you are abusing the static keyword slightly`, I saw a yellow (if not red) light blinking. But your point is well taken - I forgot to notice that there the OP is using field (and not a property). – nam Nov 26 '20 at 18:04

2 Answers2

2

As an addition to Clemens answer, I prefer it the following way:

public partial class App : Application
{
    public static App Me => ((App) Application.Current);

    public string MyProperty1 { get; set; }
    public int MyProperty2 { get; set; }
    public void DoSomething() { }
    ...
}

By that you don't have to write that casting stuff everywhere. You can now simply write code like this:

App.Me.MyProperty1 = "some value";
App.Me.MyProperty2 = 5;
App.Me.DoSomething();
Torben Schramme
  • 2,104
  • 1
  • 16
  • 28
  • 1
    Thank you for sharing your knowledge (my upvote) - I should try your suggestion ,as well. Your suggestion helps avoid casting stuff at multiple place. – nam Nov 26 '20 at 17:42
1

Since App is not a static class and an instance of that class is already accessible via the static Application.Current property, there is not need to declare any of its properties static.

Instead, declare non-static properties like

public partial class App : Application
{
    public string MyProperty { get; set; }
    ...
}

and access them like this:

((App)Application.Current).MyProperty = "Some value";

As a note, you should avoid using the as operator without checking the result for null, like

var app = Application.Current as App;
app.MyProperty = "Some Value";

because that would result in a NullReferenceException instead of an InvalidCastException, which is what you actually want to get in case the cast operation fails.

In a potential situation where as may validly return null, test for it:

var app = Application.Current as App;
if (app != null)
{
    app.MyProperty = "Some Value";
}
Clemens
  • 123,504
  • 12
  • 155
  • 268
  • This did the trick (thank you) : `instance of App class is already accessible via the static Application.Current property`. No need to declare `myVariable` as static property – nam Nov 26 '20 at 17:49