I have a method for swapping 2 Items in a list. The problem is that the ‘temp’ variable updates after I change the value of itemA. How do I prevent ‘temp’ from updating after I initialize it?
void SwapItems(Item itemA, Item itemB)
{
Item temp = itemA;
itemA.OriginItemPosition = itemB.OriginItemPosition;
itemA.OriginItemRectangle = itemB.OriginItemRectangle;
itemB.OriginItemPosition = temp.OriginItemPosition;
itemB.OriginItemRectangle = temp.OriginItemRectangle;
}
That is because temp1 holds the reference of itemA, and not it’s values copied.
To swap items in your case I would do:
You could also have something like this, which some may find nicer: