deleyd
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.)
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.)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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>.