示例#1
0
 /// <summary>
 /// Find a tasklist by ID and then return its index 
 /// </summary>
 /// <param name="observableCollection"></param>
 /// <param name="taskList"></param>
 /// <returns></returns>
 private int IndexOf(ObservableCollection<TaskList> lists, TaskList taskList)
 {
     try
     {
         TaskList taskListRef = lists.Single(tl => tl.ID == taskList.ID);
         return lists.IndexOf(taskListRef);
     }
     catch (Exception)
     {
         return -1;
     }
 }
        public static void IndexOfTest()
        {
            string[] anArray = new string[] { "one", "two", "three", "four" };
            ObservableCollection<string> collection = new ObservableCollection<string>((IEnumerable<string>)anArray);

            for (int i = 0; i < anArray.Length; ++i)
                Assert.Equal(i, collection.IndexOf(anArray[i]));

            Assert.Equal(-1, collection.IndexOf("seven"));
            Assert.Equal(-1, collection.IndexOf(null));

            // testing that the first occurance is the index returned.
            ObservableCollection<int> intCol = new ObservableCollection<int>();
            for (int i = 0; i < 4; ++i)
                intCol.Add(i % 2);

            Assert.Equal(0, intCol.IndexOf(0));
            Assert.Equal(1, intCol.IndexOf(1));

            IList colAsIList = (IList)intCol;
            var index = colAsIList.IndexOf("stringObj");
            Assert.Equal(-1, index);
        }
 public void Test_ObservableCollection_IndexOf()
 {
     var list = new ObservableCollection<int>() { 6, 5, 8 };
     Assert.Equal(0, list.IndexOf(6));
     Assert.Equal(1, list.IndexOf(5));
     Assert.Equal(2, list.IndexOf(8));
 }