As the title states. When a task is indented on a pred task that has a link (Finish To Start, haven’t tested with others), trying to outdent it to root task will result in an error.
Code to replicate:
using Aspose.Tasks;
using Aspose.Tasks.Util;
try
{
var project = new Project();
project.Set(Prj.StartDate, new DateTime(2024, 10, 7, 8, 0, 0));
project.Set(Prj.MinutesPerDay, 480);
project.Set(Prj.MinutesPerWeek, 2400);
project.Set(Prj.DaysPerMonth, 20);
AddDefaultCalendar(project);
project.CalculationMode = CalculationMode.Automatic;
var task1 = project.RootTask.Children.Add("Task 1");
task1.Set(Tsk.Start, new DateTime(2024, 10, 7, 8, 0, 0));
task1.Set(Tsk.Duration, project.GetDuration(2, TimeUnitType.Day));
var task2 = project.RootTask.Children.Add("Task 2");
task2.Set(Tsk.Duration, project.GetDuration(1, TimeUnitType.Day));
var link = project.TaskLinks.Add(task1, task2);
link.LinkType = TaskLinkType.FinishToStart;
link.LinkLag = 0;
var task3 = project.RootTask.Children.Add("Task 3");
task3.Set(Tsk.Start, new DateTime(2024, 10, 7, 8, 0, 0));
task3.Set(Tsk.Duration, project.GetDuration(1, TimeUnitType.Day));
// Indenting the task 3 to be the child of task2
task2.Children.Add(task3);
var collector1 = new ChildTasksCollector();
TaskUtils.Apply(project.RootTask, collector1, 0);
foreach (var task in collector1.Tasks)
{
Console.WriteLine($"Task Name: {task.Get(Tsk.Name)} - Task Start: {task.Get(Tsk.Start)} - Task Finish: {task.Get(Tsk.Finish)} - Task Parent Name: {task.ParentTask?.Get(Tsk.Name)}");
}
// Works and task is indented to be the child of task 2
// After outdenting the task 3 <-- this will result in an error
project.RootTask.Children.Add(task3);
// This will not run as exception is thrown: "An Item with the same key has already been added."
var collector2 = new ChildTasksCollector();
TaskUtils.Apply(project.RootTask, collector2, 0);
foreach (var task in collector2.Tasks)
{
Console.WriteLine($"Task Name: {task.Get(Tsk.Name)} - Task Start: {task.Get(Tsk.Start)} - Task Finish: {task.Get(Tsk.Finish)} - Task Parent Name: {task.ParentTask?.Get(Tsk.Name)}");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
}
void AddDefaultCalendar(Project project)
{
var cal = project.Calendars.Add("Default");
cal.WeekDays.Clear();
CreateDefaultCalendar(cal);
cal.Uid = 0;
project.Set(Prj.Calendar, cal);
}
void CreateDefaultCalendar(Calendar cal)
{
var wt1 = new WorkingTime(new DateTime(1, 1, 1, 8, 0, 0), new DateTime(1, 1, 1, 16, 0, 0));
var monday = new WeekDay(DayType.Monday);
var tuesday = new WeekDay(DayType.Tuesday);
var wednesday = new WeekDay(DayType.Wednesday);
var thursday = new WeekDay(DayType.Thursday);
var friday = new WeekDay(DayType.Friday);
monday.WorkingTimes.Add(wt1);
tuesday.WorkingTimes.Add(wt1);
wednesday.WorkingTimes.Add(wt1);
thursday.WorkingTimes.Add(wt1);
friday.WorkingTimes.Add(wt1);
monday.DayWorking = true;
tuesday.DayWorking = true;
wednesday.DayWorking = true;
thursday.DayWorking = true;
friday.DayWorking = true;
cal.WeekDays.Add(monday);
cal.WeekDays.Add(tuesday);
cal.WeekDays.Add(wednesday);
cal.WeekDays.Add(thursday);
cal.WeekDays.Add(friday);
}