Created
April 5, 2018 07:10
-
-
Save GregTrevellick/e26f86251557b8ef480ea428eafef86c to your computer and use it in GitHub Desktop.
EnvDTE Project indexing is not zero based
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Set up (full example at https://github.com/GregTrevellick/AutoFindReplace) | |
IServiceContainer serviceContainer = this as IServiceContainer; | |
DTE dte = serviceContainer.GetService(typeof(SDTE)) as DTE; | |
var projs = new List<string>(); | |
// Let's assume we have a solution containing three projects | |
// The following will crash | |
for (int i = 0; i < dte.Solution.Projects.Count; i++) // Projects.Count is '3' as we'd expect | |
{ | |
var item = dte.Solution.Projects.Item(i); // "i" | |
projs.Add(item.FullName); | |
} | |
// The following will work and populate 'projs' with three entries | |
for (int i = 0; i < dte.Solution.Projects.Count; i++) // Projects.Count is still '3' as we'd expect | |
{ | |
var item = dte.Solution.Projects.Item(i + 1); // "i + 1" | |
projs.Add(item.FullName); | |
} | |
// Of course the following will also work fine | |
for (int i = 1; i < dte.Solution.Projects.Count + 1; i++) | |
{ | |
var item = dte.Solution.Projects.Item(i); | |
projs.Add(item.FullName); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment