Duplicate Item Check

Added code to prevent a duplicate item from being entered into the list

void OnSaveClicked(object sender, EventArgs args)
{
	bool itemAlreadyExists = false;

	if (Application.Current.Properties.ContainsKey("Items")) {
		allItems = (List<Item>)Application.Current.Properties["Items"];

		Item newItem = new Item(itemEntry.Text, addToBuyList);
		itemAlreadyExists = CheckListForExistingItem(newItem);

		if (itemAlreadyExists) {
			DisplayAlert("Duplicate Item", "This item already exists in one of your lists.", "OK");
		} else {
			allItems.Add(newItem);
			itemEntry.Text = "";

			if (!addToBuyList) MessagingCenter.Send<AddItemPage>(this, "UpdateAddItemsListFromAddItems");
			else MessagingCenter.Send<AddItemPage>(this, "UpdateBuyItemsListFromAddItems");
		}
	}
}

private bool CheckListForExistingItem(Item item)
{
	if (allItems.BinarySearch(item) >= 0) return true;
	else return false;
}
This entry was posted in Side Projects, Software Development and tagged , , , . Bookmark the permalink.