Link to home
Start Free TrialLog in
Avatar of deleyd
deleydFlag for United States of America

asked on

Refactoring Pointer to Items in a List

I'm refactoring some legacy code.

The code has a class A, and a List<A> list of these items.

Elsewhere there is stored a List<int> list of integers. The integers are used to access elements from the List<A> list. Each integer is an index into the List<A>. So the integers act as pointers to elements of type A in List<A>.

I'm thinking there's got to be a better way, a more modern OOP way I can refactor this.

Given integer i from List<int>, it's not explicit that this is a pointer into List<A>. That's "god" knowledge that only the programmer knows.

I thought how about convert the List<int> into another List<A>, however I don't want to have 2 copies of the elements. (I already have that problem implicitly, as if List<A> changes, that could invalidate the contents of List<int>. List<A> needs to be one which can only grow, and never have elements removed.)

What would be a more OOP oriented design for this situation of wanting a subgroup of a List?

(Maybe it's OK to have two copies of the elements? One in the original List<A>, and the other in the sublist? It's not clear yet if the contents of A are invarient or might change.)
Avatar of Misha
Misha
Flag of Russian Federation image

Why do you need list of indexes (List<int>) ? You can apply loop foreach to List<A> to iteration its elements.
Or do you store some special indexes of List<A> ? For example, marked elements ?
Is this case you can put marked element in new list and remove it from old List<A>.
ASKER CERTIFIED SOLUTION
Avatar of Eduard Ghergu
Eduard Ghergu
Flag of Romania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial