❔ ✅ 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 clunky9 Replies
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
Well you just made me think of what is probably the obvious answer. To just have a
MyThing activeThing
fieldyes but you need to control that
there CANNOT be 2 active objects at the same time
as per your requirements
right, so whatever is the
activeThing
is the one that is activeand 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
Yeah, true
Thanks
np
!close
Closed!
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.