Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GH-3544] Add null check for Application.Current #3546

Merged
merged 1 commit into from Jul 9, 2019

Conversation

Evangelink
Copy link
Contributor

Describe the changes you have made to improve this project

Add null checks for usage of Application.Current in the main part of the library. Note that the demo and the unit tests haven't been updated as they are unlikely to be used wrapped in a WinForm App.

Unit test

No unit test added but if needed I can add a new test project with a WinForm app hosting the WPF MahApps window.

Additional context

Closed Issues

Fix #3544

var appStyle = (window.IsNull()
Theme appStyle;

if (Application.Current.IsNull()) // In case the Window is hosted in a WinForm app
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have used the IsNull() extension method here as it seems to be used in this class. If needed I could extract the methods IsNull() and IsNotNull() into a separate class and use them for the other changes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know what is better in these cases. Should we always use these 2 extension methods? Or not? I don't know...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH I am not really sure to understand what those 2 methods bring compare to having the null check. Was it to avoid some FPs from ReSharper with the null reference rules?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes this could be the reason, @batzen can explain this a little bit more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added those because writing if (!(xy is null)) feels horrible, whereas if (xy.IsNotNull()) does not. And to be consistent i also added IsNull.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@batzen I don't know whether this is a good practice or not but you could use if (x is object) to test non-nullability.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just to complete this: Since C# 7.0 you could also use this code:

        if (SomeObject is SomeType variableName)
        {
            // Do some stuff with variableName
            return variableName.ToString();
        }

More about this: https://docs.microsoft.com/dotnet/csharp/language-reference/keywords/is

Happy coding

Tim

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@timunie That's correct, and I use it more and more, because of better readable code, but sometimes you don't need extra vars ;-)

@punker76 punker76 added this to the 2.0.0 milestone Jul 9, 2019
@batzen
Copy link
Collaborator

batzen commented Jul 9, 2019

Would you mind adding your changes/fixes to ThemeManager.cs also to Fluent.Ribbon?
We (@punker76 and i) try to keep the classes in sync.

Add null checks for usage of Application.Current in the main part of the library.
@punker76 punker76 merged commit 7e685ab into MahApps:develop Jul 9, 2019
@punker76
Copy link
Member

@Evangelink your changes have been merged, thanks for your contribution 👍

@Evangelink
Copy link
Contributor Author

@batzen I'll do the PR on FluentRibbon today!

@Evangelink Evangelink deleted the app-current-null branch July 10, 2019 07:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

Application.Current: Missing Null check
4 participants