问题
I was trying to get all the error in the Visual Studio 2015 Error List window but I am getting an index out of bound exception while trying to access the ErrorItem through errors.Item(i) call. Am I missing any casting ?
EnvDTE80.DTE2 dte2 = ServiceProvider.GetService(typeof(EnvDTE.DTE)) as DTE2);
ErrorItems errors = dte2.ToolWindows.ErrorList.ErrorItems;
for (int i = 0; i < errors.Count; i++)
{
ErrorItem item = errors.Item(i);
}
回答1:
The ErrorItems.Item
method seems to need an absolute index, starting from 1, so change your loop to:
for (int i = 1; i <= errors.Count; i++)
Then it should work.
回答2:
Well the .Count()
Methods of all Lists or Arrays start at 1, while a Index in the IT world always will start at 0. So at the very end, your count will reach a number that won't be in your index.
So try the suggestion of BviLLe_Kid and subtract 1.
EDIT
Okay forget that. I think that was a brainlag. But I think deleting this answer won't be necessary since I can tell that BviLLe_Kid suggestion is wrong.
SCIENCE
static void Main(string[] args)
{
List<string> tmpList = new List<string>();
for (int i = 0; i < 10; i++)
{
Debug.WriteLine("List Item No" + i);
tmpList.Add("Item " + i);
}
Debug.WriteLine("_____________");
Debug.WriteLine("List Count: " +tmpList.Count());
Debug.WriteLine("_____________");
for (int i = 0; i < tmpList.Count(); i++)
{
Debug.WriteLine(tmpList[i]);
}
}
Debug:
List Item No0
List Item No1
List Item No2
List Item No3
List Item No4
List Item No5
List Item No6
List Item No7
List Item No8
List Item No9
List Count: 10
Item 0
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
Item 7
Item 8
Item 9
来源:https://stackoverflow.com/questions/42159829/getting-index-was-outside-the-bounds-of-the-array-while-calling-envdte80-erro