internal void Replace(string idToUpdate, Rock rock) { if (rock == null) { throw new ArgumentNullException("rock"); } int index = IndexOf(idToUpdate); if (index == -1) { throw new ArgumentException("Supplied ID not found in collection."); } //Remove the rock from the collection. Rocks.RemoveAt(index); //Verify the (possibly changed) ID not in collection already if (IndexOf(rock.UniqueId) != -1) { throw new ArgumentException("A rock with the specified ID already exists in the collection."); } //Insert the new rock at the same place. Rocks.Insert(index, rock); }
/// <summary> /// Deletes the rock with the selected ID and returns the previous element in the collection (or null if there are no more elements in the collection to edit). /// </summary> /// <param name="selectedID">The Unique ID of the rock to delete.</param> /// <returns>The next rock to be deleted (or 0 is no more rocks).</returns> internal Rock Delete(string selectedID) { if (selectedID == null) { throw new ArgumentNullException("selectedID"); } int index = this.IndexOf(selectedID); if (index < 0) { throw new ArgumentException("specified item not found in collection."); } Rocks.RemoveAt(index); int nextID = Math.Min(Rocks.Count - 1, index); //if the count is 0, this will be -1, we want to return that there is no "Next" rock. return(nextID < 0 ? null : Rocks[nextID]); }