c# - Prevent infinite loop between objects referencing each other? -


okay, wasn't sure headline fit problem, here goes description:

i have objects can reference other objects, create dropdown lists content/values dependant on values chosen in "parent" dropdowns.

my dropdown objects contain id, , parentid (and other stuff, not relevant here).

i want prevent users making infinite loops, this:

  • list 1 (dependant on list 3)

  • list 2 (dependant on list 1)

  • list 3 (dependant on list 2)

i've tried writing recursive method prevent it, cannot figure out logic.

could tell me how ensure object isn't referencing self "down line" ? or provide example perhaps.

any appreciated.

the simplest way can think of create flattened list. recursively iterate objects , store each reference in list. find new objects check each 1 in list.

you'll either encounter object referencing or run out of objects search.

this method being suitable depend on requirements, speed / memory/ number of items in list.

since object contain id list store/check instead if need check value equality instead of reference equality


Comments