MVPs any ideas on this Collection issue?

L

Laurence K

I find that looping through a tasks collection in a project which
contains sub-projects, I get different results depending on the method
used;

- if I use a For/Next loop as in "For each tsk in activeproject.tasks"
I get all tasks including all those in any sub-projects. I can
identify the sub-project tasks by for instance "if tsk.project <>
activeproject.projectsummarytask.project"

- if I use a manual loop through the same collection as in "for i =1
to activeproject.tasks.count" I get only the tasks in the main
project. These include the single summary task representing the
sub-project (which has a Task ID within the main project), but not the
subtasks of the sub-project.

I am probably being stupid, but I would expect these methods to return
the same results - syntactically they seem to be addressing the same
collection. I know from prior performance tuning that a For/Next loop
is massively faster than the "For i = 1 to activeproject.tasks.count"
approach which seems to indicate something very different is going on
under the hood, but I don't understand why I get different task
results.

This isn't a theoretical question as I have a lot of code which needs
to handle sub-projects - the difficulty is that some parts use the
For/Next approach (basically when checking all tasks) whereas other
code has to use the "For i = tskNNN.ID to activeproject.tasks.count"
approach. I have found some workarounds in code, but would like to
understand the basic reasons for the difference.

Probably a TJHIW problem.

Cheers,
L.
 
J

JackD

Laurence K said:
I find that looping through a tasks collection in a project which
contains sub-projects, I get different results depending on the method
used;

- if I use a For/Next loop as in "For each tsk in activeproject.tasks"
I get all tasks including all those in any sub-projects. I can
identify the sub-project tasks by for instance "if tsk.project <>
activeproject.projectsummarytask.project"

- if I use a manual loop through the same collection as in "for i =1
to activeproject.tasks.count" I get only the tasks in the main
project. These include the single summary task representing the
sub-project (which has a Task ID within the main project), but not the
subtasks of the sub-project.

I am probably being stupid, but I would expect these methods to return
the same results - syntactically they seem to be addressing the same
collection. I know from prior performance tuning that a For/Next loop
is massively faster than the "For i = 1 to activeproject.tasks.count"
approach which seems to indicate something very different is going on
under the hood, but I don't understand why I get different task
results.

They seem to be, but really inserted projects are not really part of the
project. They are just pointers in the file which point to the inserted
file.. So Activeproject.tasks.count returns all the tasks in the file
(including those pointer tasks). In my opinion this is correct behavior.
However, since we often want to look at ALL the tasks it appears that
microsoft realized that having to write code which has to open up all the
projects and iterate through them as well as the master project and enhanced
the scope of activeproject.tasks. From what you are saying they did this for
most cases, but not for the one you noted.

I have never has to use the "for i" construction so I haven't bumped into
this before.
This isn't a theoretical question as I have a lot of code which needs
to handle sub-projects - the difficulty is that some parts use the
For/Next approach (basically when checking all tasks) whereas other
code has to use the "For i = tskNNN.ID to activeproject.tasks.count"
approach. I have found some workarounds in code, but would like to
understand the basic reasons for the difference.

Probably a TJHIW problem.

Indeed.

-Jack
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top