0

I need a copy of a project, but I don't find a method like Clone() or other. This is my code:

 private void Change(ArrayList SecuenciasIniciales, Project pj)
        {
            Project pjTemp = pj; //not make a copy else the same reference   

            for (int i = 0; i < SecuenciasIniciales.Count;i++ )
            {
                int[] secuencia = (int[])SecuenciasIniciales[i];
                ChangeProjectTemp(secuencia,ref pjTemp,pj);

                pjTemp.Application.LevelingOptions(false, true, true, PjLevelOrder.pjLevelPriority, true, Type.Missing, Type.Missing, PjLevelPeriodBasis.pjDayByDay, true, false, Type.Missing);
                pjTemp.Application.LevelNow(true);

                if (Directory.Exists(Server.MapPath("") + "\\sample.mpp"))
                {
                    System.IO.File.Delete(Server.MapPath("") + "\\sample.mpp");
                }

                pjTemp.Application.FileSaveAs(Server.MapPath("") + "\\sample.mpp", PjFileFormat.pjMPP, Type.Missing, Type.Missing, Type.Missing,
        Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

                pjTemp.Application.FileSave();
                pjTemp.Application.DocClose();
                pjTemp.Application.Quit(PjSaveType.pjDoNotSave);

              }
}

I don't want that pj change. What is the solution?

Esneyder
  • 471
  • 1
  • 5
  • 19
  • Why on EARTH are you using an ArrayList? – It'sNotALie. Feb 15 '13 at 22:18
  • `ArrayList` on February, 2013th? Seriously? – Darin Dimitrov Feb 15 '13 at 22:18
  • EARTH?, is a Project of Microsoft Project – Esneyder Feb 15 '13 at 22:23
  • I don't think MS does code in spanish. – It'sNotALie. Feb 15 '13 at 22:49
  • 1
    What the previous commenters were trying to tell you was that you should prefer to use the generic containers whenever possible. So, prefer the [`List`](http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx) container class over the `ArrayList` class because it is type-safe. Now, I guess you're saying that Microsoft Project doesn't support generics? I don't know if that's true, but I doubt it. You should definitely look into using `List` and other generic classes in new code if possible, though. Not that that solves your problem here. – Cody Gray - on strike Feb 15 '13 at 23:29
  • ArrayList SecuenciasIniciales not is the problem, because i use this for containt int[]secuencias, has no relation with pj. – Esneyder Feb 16 '13 at 13:27

2 Answers2

0

I believe you are looking for this

Deep Cloning Objects in C#

This link here might also be of use

Deep vs shallow cloning

This code is from the link in my comment- changed to Project for your sake.

A shallow copy example which may work for you:

class Project : ICloneable
{
   //rest of your class
   int blah;
   string blah2;

   public Project()
   {

   }
   //rest of your class

   public object Clone()
   {
      return this.MemberwiseClone();      // call clone method
   }
}

And then in the code you showed do this-

  Project pjTemp = (Project)pj.Clone(); 
Community
  • 1
  • 1
Bizzle
  • 24
  • 2
  • Serializing your object is just turning it into bits and bytes. Is your object referencing other objects in memory using pointers? Or all all the components of Project unique to that specific 'Project' object? If unique- you can implement that class with the interface 'ICloneable' and use the clone function as described on the page: http://en.csharp-online.net/ICloneable scroll down to the 'Person' class. If your class has references in memory to other objects you are going to have to do all the deep copying yourself as ICloneable cant know how to deep copy every type of class. – Bizzle Feb 16 '13 at 02:02
0

There are two possible options how to make a copy of a project and both of them works to solve different tasks.

Task 1: you want to make a copy of a project to perform faster analysis of the project because COM calls are slow.

Solution: Read all you want to know about your project through MS Project COM interface, save the data into your own data structure and do whatever you want with the data structure. Alternative solution for the task could be save the project into XML and deserialize the XML into a data structure.

Task 2: you want to make a copy of the project to work with it as with new project using MS Project API. E.g. your initial project is a template.

Solution: use Application.FileSaveAs and later work with the newly created file.

It is important to understand that you manipulate your project using API to MS Project but MS Project isn't .NET application and none of the .NET features like IClonable or Object.MemberwiseClone are available for you. You can only tell MS Project what to do and it will somehow do that but nobody knows how.

melan
  • 1,688
  • 1
  • 11
  • 8
  • Ok, but the problem is that i going to generate more of 36 projects and i want to select only one, Do I have to save all projects like mpp with Application.FileSaveAs? – Esneyder Feb 16 '13 at 13:28
  • I'm sorry but I don't understand your use case. If you need to clone 36 projects to pick one of them, e.g. you have cross project links - then yes, you will have to either clone all of them or somehow find isolated subset of the projects. Good thing is that you can delete files easily. – melan Feb 17 '13 at 05:04
  • Ok. I believe that it will be the solution. – Esneyder Feb 18 '13 at 14:44