This warning has no sense!
If I want to access child property or action from parent using component reference I need public property (why should I add extra public method to modify private property) or action (especially).
I often access component's properties or actions from any place inside application, logically I need public properties or actions to have control of them.
I use public access level but VS is giving this warning.
I can use internal access level but I think it will be also against the logic based with blazor analyzer requires private.
My questions:
1. What are arguments for requesting private?
2. Why can not be used public or internal?
3. If there is good reason then what can we do in above cases?
@SteveSandersonMS
This contains cases mentioned above https://github.com/aspnet/Blazor/issues/1355#issuecomment-418591770
The analyzer that tells you to use nonpublic (i.e., private, protected, or internal) only applies to component parameters, not to any other methods, properties, or fields. So if you want to have public methods on your components you absolutely can and should - that's a totally standard pattern.
The only thing we're recommending not to do is have public parameters (i.e., [Parameter]). That's because there's only one correct way to set them, which is using the attributes on components in markup. If you try to set them any other way (e.g., from procedural C# code), the effect won't be what you want, because the receiving component won't re-render automatically. If you do want it to re-render, then give it some public method that receives the new value, assigns its own property, and calls StateHasChanged.
Finally, this is only guidance. If you don't like this recommendation, you can turn it off in your own project: https://stackoverflow.com/questions/34720443/suppressing-issues-from-roslyn-code-analyzers
Thank you.
I will disable this warning with big pleasure :)
@SteveSandersonMS the current tab set example (https://gist.github.com/SteveSandersonMS/f10a552e1761ff759b1631d81a4428c3) uses public parameters (for very reasonable reasons -- it's merely _getting_). What do you think about updating the warning so it only applies to public _setters_? (i.e. [Parameter]public string Foo { get; private set; })
I agree we should do that. PRs welcome!
@SteveSandersonMS alright, here's my stab at this! :) https://github.com/aspnet/Blazor/pull/1702
Most helpful comment
The analyzer that tells you to use nonpublic (i.e.,
private,protected, orinternal) only applies to component parameters, not to any other methods, properties, or fields. So if you want to have public methods on your components you absolutely can and should - that's a totally standard pattern.The only thing we're recommending not to do is have public parameters (i.e.,
[Parameter]). That's because there's only one correct way to set them, which is using the attributes on components in markup. If you try to set them any other way (e.g., from procedural C# code), the effect won't be what you want, because the receiving component won't re-render automatically. If you do want it to re-render, then give it some public method that receives the new value, assigns its own property, and callsStateHasChanged.Finally, this is only guidance. If you don't like this recommendation, you can turn it off in your own project: https://stackoverflow.com/questions/34720443/suppressing-issues-from-roslyn-code-analyzers