public static int SearchFreeID(string articleMode, ObservableRangeCollection<ArticleProperty> aPropList) { int minusOne; if (articleMode == "Outlay") minusOne = -1; else minusOne = 1; int id; for (int i = 1; ; i++) { try { id = ((int)aPropList.LongCount() + i) * minusOne; } catch (ArgumentNullException) { return 1 * minusOne; } if ((int)aPropList.LongCount() == 0) return 1 * minusOne; for (int n = 0; n < (int)aPropList.LongCount(); n++) { if (aPropList[n].ID == id) break; if (n == (int)aPropList.LongCount() - 1) return id; } } }