Have an object which is an array (not arraylist or generic) that could hold a set of anything...
[[One],[Two],[Three],[Four]]
Want to move [Four] to in front of [Two] e.g. oldIndex = 3, newIndex = 1 so the result would be...
[[One],[Four][Two],[Three]]
Whats the most effient way to do this in .NET 2.0, e.g.
PropertyInfo oPI = ObjectType.GetProperty("MyArray", BindingFlags.Public | BindingFlags.Instance);
object ObjectToReorder = oPI.GetValue(ParentObject, null);
Array arr = ObjectToReorder as Array;
int oldIndex = 3;
int newIndex = 1;
//Need the re-ordered list still attached to the ParentObject
thanks in advance
void MoveWithinArray(Array array, int source, int dest)
{
Object temp = array.GetValue(source);
Array.Copy(array, dest, array, dest + 1, source - dest);
array.SetValue(temp, dest);
}
Try this
Array someArray = GetTheArray();
object temp = someArray.GetValue(3);
someArray.SetValue(someArray.GetValue(1), 3);
someArray.SetValue(temp, 1);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With