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

[Question] SetProperty not overloaded property in .NET 6 ? #62

Closed
Lightczx opened this issue Dec 14, 2021 · 3 comments
Closed

[Question] SetProperty not overloaded property in .NET 6 ? #62

Lightczx opened this issue Dec 14, 2021 · 3 comments
Labels
by design Some behavior that is intended and not an issue mvvm-toolkit 🧰 Issues/PRs for the MVVM Toolkit

Comments

@Lightczx
Copy link

Lightczx commented Dec 14, 2021

Microsoft.Toolkit.Mvvm.ComponentModel.ObservableObject

I have a .NET 6 project with nullable enabled

I tried to apply this method in one of my property

SetProperty<T>(T oldValue, T newValue, Action<T> callback, [CallerMemberName] string? propertyName = null)

and I wrote something like below

SetProperty(ref selectedRole, value, t => { });

but VS seems to recognize my code into

SetProperty<T>(T oldValue, T newValue, IEqualityComparer<T> comparer, Action<T> callback, [CallerMemberName] string? propertyName = null)

this wrong method overload

@ghost
Copy link

ghost commented Dec 14, 2021

Hello Lightczx, thank you for your interest in Windows Community Toolkit!

I have automatically added a "needs triage" label to help get things started. Our team will analyze and investigate the issue, and escalate it to the relevant team if possible.. Other community members may also answer the question and provide feedback 🙌

@Sergio0694 Sergio0694 transferred this issue from CommunityToolkit/WindowsCommunityToolkit Dec 14, 2021
@Sergio0694 Sergio0694 added the mvvm-toolkit 🧰 Issues/PRs for the MVVM Toolkit label Dec 14, 2021
@Sergio0694
Copy link
Member

It seems like you're invoking with SetProperty with incorrect arguments, VS is correct here.
The signature you showed takes both T parameters by value, but you're passing the first one by ref, which is incorrect.
You should change your call to:

SetProperty(selectedRole, value, t => { });

Which will bind to the method you mentioned 😊

As a side, I would not recommend using that specific overload unless absolutely necessary.
Use the one taking a backing field if available, or use the one also taking a target model if you're proxying a property.
It will remove allocations and result in better performance.

Closing this as the code you showed doesn't seem to be an actual issue in the MVVM Toolkit.

@Sergio0694 Sergio0694 added the by design Some behavior that is intended and not an issue label Dec 14, 2021
@Lightczx
Copy link
Author

thanks a lot, it works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
by design Some behavior that is intended and not an issue mvvm-toolkit 🧰 Issues/PRs for the MVVM Toolkit
Projects
None yet
Development

No branches or pull requests

2 participants