Recent

Author Topic: Which is the proper way to clone a unit  (Read 3473 times)

hakelm

  • Full Member
  • ***
  • Posts: 112
Which is the proper way to clone a unit
« on: February 08, 2015, 09:16:22 am »
Sometimes you want to copy or clone unit containing a form in order to modify it and then use both units in the same project.
It doesn't seem OK to just do "Save as" as you may get "Error: Duplicate resource: Type = 10, Name = THPLOTFORM, Lang ID = 0000" when compiling.
So what is the proper way to do this?
H


Basile B.

  • Guest
Re: Which is the proper way to clone a unit
« Reply #1 on: February 08, 2015, 09:22:45 am »
Menu File, New..., in the tree you have the node inherited project components.

You can select  one of the current project form.

But keep in mind that's object oriented. For example if you change the ancestor form then it's reflected on the descendant, even in the lfm script.

hakelm

  • Full Member
  • ***
  • Posts: 112
Re: Which is the proper way to clone a unit
« Reply #2 on: February 08, 2015, 09:39:03 am »
Thanks, but I can't get this to work. The inherited project component list is in my case empty:

GetMem

  • Hero Member
  • *****
  • Posts: 3510
Re: Which is the proper way to clone a unit
« Reply #3 on: February 08, 2015, 11:08:41 am »
@hakelm

Quote
Sometimes you want to copy or clone unit containing a form in order to modify it and then use both units in the same project.
If you have two, somewhat different forms with similar functionality, you should inherit them from a common ancestor. Implement the common stuff in the base class then add specific features in each child class.

Quote
Thanks, but I can't get this to work. The inherited project component list is in my case empty:
Did you add those base units/forms to the project?
As a quick workaround copy the lfm and the pas file to a separate directory. Close your project. Open the pas file. Rename it to something else(don't forget to rename the form too). Copy back the files to the project directory. Go to project inspector and add the newly renamed files to the project.

hakelm

  • Full Member
  • ***
  • Posts: 112
Re: Which is the proper way to clone a unit
« Reply #4 on: February 08, 2015, 02:18:52 pm »
Thanks again. 
Yes I had all of my form-files added to the project but none of them showed up (now the cloned form appears but none of the others).
I had tried your "quick workaround" but did the mistake to open the files under the original project. Following your instructions everything now is OK.
It wouldn't be bad to have a functionality like this incorporated into the Lazarus IDE as a wizard or something. It is not always inheritance is the right way to go.
H

hakelm

  • Full Member
  • ***
  • Posts: 112
Re: Which is the proper way to clone a unit
« Reply #5 on: February 09, 2015, 09:46:06 am »
After some experimentation I found that the procedure described earlier is not really necessary.
The following seems to work:
Save the unit you want to clone, unit1, as unit2. Now unit2 automatically replaces unit1 in your project. In unit2 change the name property of the form to form2 and you are all set and can again include unit1 into your project.
H

Awesome Programmer

  • Sr. Member
  • ****
  • Posts: 421
  • Programming is FUN only when it works :)
    • Cool Technology
Re: Which is the proper way to clone a unit
« Reply #6 on: June 06, 2019, 08:13:17 pm »
So, basically... all you do is open the unit file you want to clone or copy of, then do SAVE AS. Lazarus then automatically creates .pas .lfm .lrs file and then you need to change their properties. That's what have been doing myself all these years. Thank you... :)
« Last Edit: June 06, 2019, 08:39:25 pm by Awesome Programmer »