dotnet icon indicating copy to clipboard operation
dotnet copied to clipboard

Inherited ViewModels can hide important warnings

Open MisinformedDNA opened this issue 3 years ago • 3 comments

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:

  1. The pragma is enabled for this warning

    • Pros: Warning might alert the user to unintended functionality
    • Cons: User can't suppress the warning
  2. Option 1 plus ObservableProperty is 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

MisinformedDNA avatar Sep 20 '22 20:09 MisinformedDNA

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 
{
}

mikechristiansenvae avatar Oct 03 '22 18:10 mikechristiansenvae

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.

MisinformedDNA avatar Oct 03 '22 18:10 MisinformedDNA

Ah! Fair point!

I agree, you are correct. (Additionally, I've replicated the issue)

mikechristiansenvae avatar Oct 05 '22 14:10 mikechristiansenvae