C
C#5mo ago
Tantrim

INotifyPropertyChanged

I have a List<ClassSpecModel> and I attach the property changed event like below in the image. I only have 1 "active" ClassSpecModel at a time that I make changes to. My question is, when I swap to a different ClassSpecModel in the list, and I attach the ClassSpecModel.PropertyChanged like shown in the image to the new ClassSpecModel, does the original ClassSpecModel.PropertyChanged become orphaned that I originally attached? Do I need to detach the ClassSpecModel.PropertyChanged in the original ClassModelObject Please let me know if this is clear or not, thanks.
No description
21 Replies
SpReeD
SpReeD5mo ago
You don't attach or deattach, you subscribe or unsubscribe to an event, meaning it's a 1:n relation.
Tantrim
TantrimOP5mo ago
You are correct, sorry for my inaccurate vocabulary choice. Do I need to unsubscribe the original subscriber? If so, I'm strugglying to find the syntax
SpReeD
SpReeD5mo ago
It depends on the rest of the code, generally speaking, "yes". However, I guess you're using the INotifyPropertyChanged in WPF/MAUI? - If so, you don't subscribe directly to the event, let the UI handle it by using bindings between View & ViewModel - also you don't wanna use List, but ObservableCollection or better BindingList since both implement the INotifyCollectionChanged interface.
Tantrim
TantrimOP5mo ago
from my understanding, ObservableCollection doesn't notify when an item inside of a list is modified only on add or remove type functions of new items into list
SpReeD
SpReeD5mo ago
Idd, that's where BindingList joins the room, also it relays when its items has implemented the INotifyPropertyChanged interface.
Tantrim
TantrimOP5mo ago
and I'm using a library called Config.Net which stores settings locally in json files. I'm using INotifyPropertyChanged to keep the Config.Net class in sync with my actual data model that I manipulate I tried using the Observer pattern but that couldn't see updates from within the lists. I need to look into BindingList maybe
SpReeD
SpReeD5mo ago
and I'm using a library called Config.Net Doesn't matter, that's BusinessLogic part, let the ViewModel contact what's behind This answer explains BindingList it in short terms: https://stackoverflow.com/questions/4284663/difference-between-observablecollection-and-bindinglist
Tantrim
TantrimOP5mo ago
this is a .Net Maui Blazor Hybrid app that I'm only targetting windows with
SpReeD
SpReeD5mo ago
Sounds strange, but the config is for your MAUI app?
Tantrim
TantrimOP5mo ago
I'm using JSON as local data storage. I'm using Config.Net to just make it easier to manipulate the JSON. It's a small application with about 156kb of data spread between around 40 JSON files I didn't want to use a database. I'm not an expert in this by any means but it seemed logical to me.
SpReeD
SpReeD5mo ago
40 json files? - why not use a DB? Serializing is fine for user-settings etc. but when it comes to data storage you wanna use an actual db.
Tantrim
TantrimOP5mo ago
maybe SQLite would have been best? I would prefer something very portable without needing a db installed on the machine
SpReeD
SpReeD5mo ago
SQLite is the way to go, it's a file-based SQL database, no need for a server, can be used with all common ORM like Dapper and EntityFramework.
Tantrim
TantrimOP5mo ago
individual JSON files will potentially be shared between users as well. The JSON read/write overhead isn't too much of a concern for this app. But I do understand storing data in JSON is far from best practice
SpReeD
SpReeD5mo ago
You may import/export (exchange) JSON objects between users by using Base64 (or Base85) coding to improve sharability and performance.
Tantrim
TantrimOP5mo ago
you definitely have me questioning my choice of JSON storage lol. I think I will go ahead with storing in JSON but I might refactor to using SQLite later if I get a wild hair.
Tantrim
TantrimOP5mo ago
if I subscribe to an event using an object of a model that I'm setting like in the image below. Do I need to unsubscribe when I subscribe again with a new model that overwrites the variable of the last one? I call the SubscribeToModel every time the working object model changes. I only work on 1 object at a time. I understand how variables of classes are just references correct? This makes me think I SHOULD unsubscribe. However, it doesn't seem to fire the event when I overwrite the variable as shown in the image and the modify the original object directly in the list without modifying it using the lassData.CurrentClassSpecSelectedModel variable.
No description
SpReeD
SpReeD5mo ago
variables of classes There's nothing like it, you have either Properties or Fields, the latter are the actual storage, while a Property is like a linked gate to a shadow-field/backing-field behind it. Going further, this is the so called state of the instance of the object, which changes throughout runtime. While static is a non-instance object, so every method or field has only one instance, like a singleton pattern styled class. I wrote this, because I see you have plenty of static, if not only, methods. Can you provide more $code ?
MODiX
MODiX5mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
Tantrim
TantrimOP5mo ago
thank you for the vocabulary update. the field ClassData.CurrentClassSpecSelectedModel is static. That probably explains the SubscribeToOverrideModels is the same pattern as SubscribeToSpellModel so I don't think it needs to be shared. Also I'm not subscribing to events with ClassData.CurrentClassSpecSelectedSetting so it's mostly irrelevant I think
No description
No description
No description
Tantrim
TantrimOP5mo ago
No description
Want results from more Discord servers?
Add your server