Best way to re-use code

the OP stated he uses git
so I offered a solution that worked with that, retains text diffs, and all the upside of using git and doesnt have the downside that Xojo externals do

Xojo externals work - to a point and then when you need something they cant do (like classes in modules) you have to do something else

[quote=483789:@Rob Hallock]How do you write code once, and use it in multiple projects?

For example, let’s say I have an AddressBook class and I use it in my Desktop app. I want to also use that class in an iOS version of the app, which is in a different project.

I could just drag a copy of that class from one project to the other, which I have been doing.

Is there a better way? The problem is that if I change the class in one project, I have to remember to do it in the other.

Is there a “best practice” for this?[/quote]

[quote=483851:@Norman Palardy]the OP stated he uses git
so I offered a solution that worked with that, retains text diffs, and all the upside of using git and doesnt have the downside that Xojo externals do

Xojo externals work - to a point and then when you need something they cant do (like classes in modules) you have to do something else[/quote]
It does? The post you linked came after mine. Have a great day, Norman.

using git he retains all the advantages he has of using git without the downsides of Xojo externals
in fact it lets him do things Xojo externals cannot do
continuing to post “use xojo externals” ignores HIS situation and the capabilities he has already

[quote=483841:@Anthony Cyphers]I haven’t tested in a long time, but if you use Externals in Xojo, I think the best way is/was:
1.) Make external in Project 1
2.) Save Project 1
2.) Copy it in the navigator of Project 1
3.) Paste in to the navigator of Project 2
4.) Make external in Project 2 at the same path
5.) Save Project 2[/quote]
Maybe I’m confused (which is not surprising) about what @Rob Hallock is trying to do, but according to Sharing code among multiple projects — Xojo documentation where it says:

You can also add an external item by dragging the file from the disk to the Navigator while holding down ?+Option (on Mac) or Shift+Ctrl (on Windows and Linux). 

There is no need for “copies” of the External. By ?+Option+dragging the External Item file from Finder into the IDE Navigator you create a “link” to the original External Item file. This way only one version of the External Item exists to be shared among projects. If a change is made to the External in one project, that change is reflected in the other projects linked to the same External Item.

As @Norman Palardy mentioned, working with External Items has it’s quirks, but it does work. For myself, I put all my External Items under a GitHub project (separate from my other projects). The annoying part of working with Externals in this case (with 2019r3.1 anyway) is that:
A.) - Sometimes just opening a project linked to an External Item (saved in xml format) will cause the properties of an object in the Item file to get reordered (but fundamentally not change). Which GitHub records it as a file change. I mention this in <https://xojo.com/issue/58751>
B.) - It appears you cannot copy-and-paste components like properties or methods between Externals or project embedded objects.

But the benefits are having reusable code with one source file, and if two projects are open (that share the same External) and a change is made to the External in one project - the other project will immediately pick up the change.

I hope that helps.

The way I outlined, if I remember correctly, also allows for modules with embedded classes or other modules, while it doesn’t work as expected otherwise.

As I said, though, I’ve not had a need to do this in quite some time, so it may be different and YMMV.

It looks like modern Xojo entirely blocks making and adding an external module with child classes/modules. If you try, you get messages. You can export the module, but you cannot then import it in to a new project as an external.

I noticed that too, when I was trying to incorporate your excellent GraffitiColors module as an External Item to some of my projects. I had to rework things to put the parts I wanted all as direct child objects from the one parent Module. Then it worked fine :slight_smile:

Xojo externals are useful up to a point
And then when they turn out to not be you need to move to something else
For us that was day 1 and we’ve used svn externals since the first time we needed to share code between projects as they need to say in sync

I’m just suggesting for those than have the ability to use text projects that they should get used to using their version control system for this and retain more history in a usable form

YMMV

In the past I’ve used extrnals a lot and often and it is very helpful if you are working on projects alone or with restricted user groups with same philosophy and project spirit and knowledge. But this can become deadly if somebody changes something you are not aware of.

Since 2019 I’ve begun to move my objects and code snippets to an internal git Instance. I love https://gitea.io/ which is lightweight, blazing fast and easy for collaborative work with others.

git or svn can be run on a single machine quite readily regardless of whether you’re using macOS, Windows, or Linux

in case you need a nice UI for git: https://www.git-tower.com/

Well, native git support within XOJO would be Eastern, Birthday and Christmas in a single day.

heh … SVN ! reminiscent of the vi vs emacs wars :slight_smile:

I’d sure want it to be “pluggable” so you could select git, I could pick git OR svn depending on project etc

If a person could write a “Version control script” that the IDE could use that’d be almost ideal then it would be infinitely extensible by anyone for whatever version control system they used

And if there wasnt one installed then the IDE just acts like it does now

<https://xojo.com/issue/11544>

have you ever heard about using libraries ?
But Xojo has more importend work to do, like Android …

2 Likes

Boring that merely re-opening this makes it show up when it has no new content.

Patience, my friend. Patience.

5 Likes

For me, the external does not solve the problem.
For the simple reason that the objects are different when trying to use them on different platforms.

For example, a weblLstBox class cannot copy to iOSMobileTable. Notice that both objects are different.

The ideal thing for me, as Johann Kienbrandt mentions, is to use a class library.

I hope XOJO planning a class library where the team can save objects and share them. Without cross-project problems.

@TimStreater I appreciate your interest in this topic.

There (always | often) is another way or approach… and there will be new content for this topic. Stay tuned for a little surprise :angel:

2 Likes

Shameless plug for a video tutorial I did recently (ish) on using external modules and classes in Xojo: Using External Modules & Classes - YouTube.

I use external classes all the time and they work well for me.

4 Likes