C
C#2y ago
Relevant

❔ ✅ Choosing an appropriate data structure

So I have a list of objects and I want one in the list to be active and the rest to be inactive. I was thinking of using a Dictionary<Thing, bool> where the bool is if it's active or not. I guess this would work, but then I'd have to do something like myDict.FirstOrDefault(d => d.Value == true).Key which feels a little clunky
9 Replies
Henkypenky
Henkypenky2y ago
active in your case is just a figure of speech how would one approach such a problem you can have the list, and have the first be the active one or the last one or you can add a property to the object to mark it as active there is no data structure where only 1 object is active, this is more like a personal thing you can also consider a queue with priority, where the highest priority is the active object, and having DequeNormal and DequePriority what i'm trying to say is you either explicitly say the object is the active, adding a property to the object, or you manipulate the data structure to your advantage and document what, where and why the "active" object is
Relevant
RelevantOP2y ago
Well you just made me think of what is probably the obvious answer. To just have a MyThing activeThing field
Henkypenky
Henkypenky2y ago
yes but you need to control that there CANNOT be 2 active objects at the same time as per your requirements
Relevant
RelevantOP2y ago
right, so whatever is the activeThing is the one that is active
Henkypenky
Henkypenky2y ago
and if you try to add another to a collection, whichever it might be, check if it's active or not and check against the collection aswell
Relevant
RelevantOP2y ago
Yeah, true Thanks
Henkypenky
Henkypenky2y ago
np
Relevant
RelevantOP2y ago
!close
Accord
Accord2y ago
Closed! Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server