How to use an external method

Hi. I am trying to learn how to use external methods and I can’t quite figure it out.

I have created an external method to open and return a database connection. This external program file is located in a sub-folder named External; the name of the file is syOpenDB.xojo_binary_code and the method it contains is OpenDB. In a new program I have added an external method and I tried to point it to my external method. I set the method name to OpenDB and I have tried countless values for Lib, including the full path and program name. I have also tried it with setting the “Soft” check box on and off. When I attempt to compile the new program it reports “this item does not exist”.

dim db as Database = OpenDB 'I have also tried OpenDB()

What I am doing wrong? I tried to locate an example project that uses an external method but I could not find one.

John

I guess my first question would be why are you trying to use an external program to open and return a db connection? Why not just open the database connection from the program?

Why not? I will have numerous standalone programs (separate executables) that will need to connect to the same database.

In my further efforts to resolve this I found if I reference OpenDB as app.OpenDB I no longer get the item not found error. Instead I get a message that external functions cannot use objects as parameters, even though I don’t have any parameters on the method, just a return type of Database.

External methods are for API calls (DLLs on Windows, dylibs on Mac/Linux) not for method calls in other programs. I think this is where your confusion is arising. Check the docs to get a better understanding of using external methods. Page Not Found — Xojo documentation

[quote=58824:@John Schmoyer]
In my further efforts to resolve this I found if I reference OpenDB as app.OpenDB I no longer get the item not found error. Instead I get a message that external functions cannot use objects as parameters, even though I don’t have any parameters on the method, just a return type of Database.[/quote]
A return type of Database IS an object - thats a no no

But - see what Jason pointed you to “external method” ? “external program”

Well, this is confusing. The Declare documentation Jason pointed me to indicates that is used for API calls. I don’t want to do an API call, I just want to have some Basic code that is shared among numerous Xojo programs. I thought that was done by the “Make External” option in the IDE, and the “External Methods” option would point to my external method. If “External Methods” is not what I should be using, how do I reuse my external method without having a separate copy in every program?

Ah, I see the issue, confusing terminology.

“External Methods” refers specifically to API calls and has nothing to do with storing your modules or classes external to your project.

Once you create a class, export it to a binary or XML, delete it from your project, then drag the file you created back in while holding down command-option (Mac — I don’t recall the equivalent Windows/Linux keys). The item will appear in italics and that means that it is external to your project.

You can also use the “Make External” option which should do all that for you. (I just like doing it the long way.) For new or other project, again, drag the file into that project while holding down those keys and it will be accessed externally.

Thanks Kem. I was able to drag my external something (class?) into my new program, and then I had two “App” entries - one for the separate class and the other for the new program. I renamed the separate class syOpenDB to make it clearer. But …

dim db as Database = syOpenDB.OpenDB

This gives me an error “this item does not exist”. Perhaps I dragged the class to the wrong place? I have the scope set to “Public” on the added class, so I don’t quite understand why the item doesn’t exist. The dim db is an action event of a button on Window1.

Is syOpenDB a class or module? (blue cube or globe) If it is a class is OpenDB a shared method? It would need to be if you do not create an instance of syOpenDB before calling the method. The simplest solution might be to try the following instead:

dim syODB as new syOpenDB
dim db as Database=syODB.OpenDB

This first creates a new instance of syOpenDB before using one of the methods. If the constructor of syOpenDB takes parameters, you will need to add them when you create the new instance.

Hi Jason. That’s a good question. The icon looks more like a computer screen with a protractor or something on it. It’s superclass is “application”. I right clicked on the OpenDB method and it has a “convert to shared” option so it must not be shared. But I tried the dims you suggested and that worked. Thanks!

I am not sure where I should drag an external class. I dragged to the App option in my new program. Maybe that is not the correct place to drag it because adding it seems to have confused the menu control. Since this is a test program at this point I just deleted the menu control.

You cannot drag in an entire project, or an app object. Put your db code in a Module in the Externals project, then EXPORT the module. That gives you a file that you can drag into another project.

Thanks Tim - that works great. Dragging the entire project caused problems and eventually Xojo couldn’t open my project (“Fatal error”) so I had to rewrite it. Probably the IDE should not allow dragging an entire project into an existing one, but it does.

Is there a way to just reference to the exported code module rather than drag it as “aboslute” in the current project.

Hold down the modifier keys (Control-Shift on Windows) while you drag it in and it will be an external reference.

The users guide contains a lot of fun little tips like this :stuck_out_tongue:

“FTFM” you say >_<

He Norman, I just heard you with Paul in a XojoTalk podcast. You are the ultimate IDE specialist I learned.
It took me some time to get used to it, but I with a lot of handy things under the hood, I got more and more respect for the huge amount of work and experience put into it.
But, yes, I do still have some wishes… >_<