Where to put declares?

I am moving forward from this other post which I will solve it. In addition to this, I also have a whole chunk of declares that is in a module (VB6) or a .vb file (.NET) to be added to the VB6 or VB.NET project. This will make the declares available to the project as the declared functions can be called from anywhere.

I looked at the Xojo provided example projects, SetWindowIcon and DeclareDrawing and see the declares are put in the Button Action handler. I am trying to figure out where I should put the declares so that I don’t have to duplicate them. I have a read of modules and even tested moving the declares into a module but seems I am not doing it right or that may not be the right place.

Edit: At a later stage, I will recompile the DLL C source into OS X and will also need to include the declares.

Appreciate any advice that will kick me forward.

Thanks.

I forgot to mention that the VB6 modules and the .vb file also include contants and types (structures).

I would recommend a module. If it’s a simple one liner, then use a “External Method” otherwise create a method.

Thanks Sam,

Maybe I include some examples from the module file. Meantime I am reading through the docs and looking through examples.

There are more than 100 of “Function” similar to this (of course different function name, parameters and return):

Public Declare Function fmFindDate Lib "taSuite" (ByRef b1 As fmBars, ByVal Date1 As Double) As Long
There are a few of “Sub”, an example:

Public Declare Sub fmFreeArray Lib "taSuite" (ByRef a1 As fmArray)

There are a quite a few of “Constants” group, an example:

'Return Value Constants Public Const fmSuccess = 0 Public Const fmErrSuccess = 0 Public Const fmErrInvalidParm = -1 Public Const fmErrFileOpen = -2 Public Const fmErrMalloc = -3 Public Const fmErrFileRead = -4 Public Const fmErrFileWrite = -5 Public Const fmErrFileSeek = -6 Public Const fmErrFileExists = -7 Public Const fmErrFileNotFound = -8 Public Const fmErrRecordNotFound = -9 Public Const fmErrUnknown = -10

And quite a number of “Type”, an example:

'Array Type Defs Public Type fmArray datapointer As Long Size As Long datasize As Long firstvalid As Long End Type

Yes, the external methods introduced with 2015r3 are the best place for it. You can keep them in a module or class, taking the ptr to the object and additional parameters. As long as you stay in the ObjC framework of OS X, check “ObjC” and put the selector in the appropriate field. For C type methods not using a selector, keep ObjC unchecked and don’t input a selector. That’s IMHO the fastest way to declares now in Xojo.

Addition:
Functions and subs are both methods. The difference is functions have a return value, subs don’t. Finddate should look a bit like that:

with the exception that Long should be translated to what that is under your system – they differ a bit in their interpretation. #taSuite is best kept as a text or string constant inside your module so the string is only declared once.

And I don’t know what fmBars are. I guessed here an object where a ptr could be the thing to forward. Could very well be a structure or anything else; please check the documentation.

fmFreeArray would look almost the same, only without a return value and fmarray being a structure of the kind you describe. Will it really be returned (Byref) or is it a simple call on that structure?

The constants you describe seem to be well kept in an enumeration where you can assign the integer values after naming the elements. See this picture for a start – and check in the documentation that integer and not something like Int32 is the right data type for this enumeration:

Hi Ulrich,

Thank you for taking the time with the detailed explanation. I spent the last 2 hours checking through and I now get where to put the declares:

  1. Insert a Module and then into this Module…
  2. insert External Methods… one each for each Declare.

I experimented with the SetWindowIcon example and move the Declares to Module/External Methods and it works. After your explanation, I also see how this is being done in another example, WindowOpacity.

Now I am facing a problem - I counted and there are 365 Declares so I have to do Step (2) 365 times. Even though I can copy-paste part-by-part of a declares is still not efficient. To add to this, there are 22 Structs and a lot of Constants. Then only after I have done all these then I can get to test whether the migration is working for me or not.

If only there is an easier way like the standard way of include a file.

[quote=243193:@Cho Sing Kum]
If only there is an easier way like the standard way of include a file.[/quote]
Its only standard in C and C-like languages
There’s no such thing in Xojo and either way you still have to write them correctly

The BEST you could do is create one, save your project as Xojo Project, which is a textual format, then edit that file with a text editor
And hope you get all the markup right otherwise your project might not load at all

Hi Norman,

I think it is not true that include a file is standard only in C and C-like languages.

While I try not to mention Xojo competitors here in this forum but then this is being done in PureBasic and PowerBASIC. Furthrtmore, it does not have to be line that say include, inc, use, using, etc. In VS, just adding a module (.bas for vb6, .vb for vb.net or .cs for C#) that contains all the required will do.

Yes, I understand now that there is no such thing in Xojo.

I did save the file SetWindowIcon example as .xojo_project after I have made the changes and looked into Module1.xojo_code to see whether I can just edit the “text” file and then I see all the #tag ExternalMethod and #viewBehaviour and I paused again. More unknown for me to find out.

An “include file” doesn’t skip the nee to write them

Open the saved file, duplicate the lines from #tag ExternalMethod to #tag EndExternalMethod (which is the only piece you need to worry about) and change the declaration in between to write a new one

Do one then open the project again, see that it comes in correctly to get the hang of it
Then write as many as you want that way
But get the syntax right or they wont work as expected when you do open the project

I wrote the post before I see your edited reply.

This is what I tried to find out.
When do I need to change the Flags = blablabla
So I can ignore the #tagviewBehaviour?

You shouldn’t need to change anything else
For each external simply duplicate the lines starting with #tag ExternalMethod up to & including #tag EndExternalMethod

[quote=243196:@Cho Sing Kum]In VS, just adding a module (.bas for vb6, .vb for vb.net or .cs for C#) that contains all the required will do.
[/quote]
And you can do that in Xojo, too.

You might want to check Jason King’s DeclareMaker project (somewhere here in this forum). It’s written for iOS but I am sure one could tweak it to work with a different documentation style and make it write to a declare module project file instead of creating “manual” declares.
It will not save you from changing parameters manually but you could save a lot of typing time.

Yes, the pluspoint of many C-based development systems is they usually come with a lot of include libraries. We have to build them ourselves – or hope on the generosity of others who share what they created (wink wink).

[quote=243291:@Ulrich Bogun]You might want to check Jason King’s DeclareMaker project (somewhere here in this forum). It’s written for iOS but I am sure one could tweak it to work with a different documentation style and make it write to a declare module project file instead of creating “manual” declares.
It will not save you from changing parameters manually but you could save a lot of typing time.

Yes, the pluspoint of many C-based development systems is they usually come with a lot of include libraries. We have to build them ourselves – or hope on the generosity of others who share what they created (wink wink).[/quote]

Like MacOSLib and Windows Functionality Suite, or iOSLib and iOSKit ?

@Cho Sing Kum :

As you mention existing VB. NET code, you may want to check http://www.xojo.com/blog/en/2014/01/accessing-net-code-from-xojo.php to make your modules into a DLL and call that from Xojo. That could spare you rewriting all.

Hi Ulrich,

Thanks. I think I explore something in that area. But then now I know I can just edit the module “text” file. May be write a script to read the declares from source and then append/write to another file that will become the Xojo module file.

Hi Michel,

Thanks for the link. The file actually does not contain program codes. It is just a module containing the declares, types and constants that make the DLL workable. Therefore this module I should prevent it from becoming “public”. If it were to be converted to a .NET DLL, it can be easily reverse engineered and the actual source will be revealed. This will not be right for me to do to the 3rd-party developer unless I spend $$$ on a .NET obfuscator and this does not make sense to my pocket unless I migrate to the .NET route then this would be a different story.

Also I don’t want to burden my users to compulsory have .NET (or a specific version of .NET) installed on their computers.