Inherited ViewModels can hide important warnings
Describe the bug
If an ObservableProperty is created in two ViewModels, a parent and child, then the parent property gets hidden and the warning is blocked from the user.
Regression
No response
Steps to reproduce
For this code:
public partial class ParentViewModel : ObservableObject
{
[ObservableProperty] private int _someValue;
public int AnotherValue { get; set; }
}
public partial class DerivedViewModel : ParentViewModel
{
[ObservableProperty] private int _someValue;
public int AnotherValue { get; set; }
}
The following warning is generated:
> 'DerivedViewModel.AnotherValue' hides inherited member 'ParentViewModel.AnotherValue'. Use the new keyword if hiding was intended.
Expected behavior
The following warning should also be generated:
'DerivedViewModel.SomeValue' hides inherited member 'ParentViewModel.SomeValue'. Use the new keyword if hiding was intended.
Screenshots
No response
IDE and version
VS 2022
IDE version
17.3.0
Nuget packages
- [ ] CommunityToolkit.Common
- [ ] CommunityToolkit.Diagnostics
- [ ] CommunityToolkit.HighPerformance
- [X] CommunityToolkit.Mvvm (aka MVVM Toolkit)
Nuget package version(s)
8.0.0
Additional context
This is due to warnings being disabled with
#pragma warning disable
Fix options:
-
The pragma is enabled for this warning
- Pros: Warning might alert the user to unintended functionality
- Cons: User can't suppress the warning
-
Option 1 plus
ObservablePropertyis modified to accept a parameter that says whether hiding is desired- Pros: The warning is exposed and can be suppressed
- Cons: May not be worth the complication
Help us help you
Yes, I'd like to be assigned to work on this item
I'm not understanding why this would be considered a bug with CommunityToolkit.Mvvm. The warning that is produced is accurate.
I'm trying to understand why you want to do this? Hiding properties and stuff is almost certainly a bad idea.
Since you're not changing the generated properties or anything, the derived class doesn't need to re-define the properties.
Why not simply do this:
public partial class ParentViewModel : ObservableObject
{
[ObservableProperty] private int _someValue;
public int AnotherValue { get; set; }
}
public partial class DerivedViewModel : ParentViewModel
{
}
The warning that is produced is accurate.
@mikechristiansenvae The bug is that the warning is not produced. And like you said, "Hiding properties and stuff is almost certainly a bad idea." I'm saying that the warning should be produced.
Ah! Fair point!
I agree, you are correct. (Additionally, I've replicated the issue)