module of personal function
Posted
#1
(In Topic #1988)
Trainee

I would like to do the following, but I don't know if it's possible.
In VB, I could have a module that contained:
Static Public Function alltrim(toto As String) As String
Return Trim(toto)
End
and in my code I could do
implements baseric2
Public Sub main()
Print alttrim("bonjour")
End
How can I do the same thing with Gambas3? And is it even possible?
Regards
Posted
Enthusiast


In Gambas, just type this:
Code
Dim s As String = " Hello world "
Print Quote(Trim(s))
Posted
Trainee

I have a multitude of functions such as calculating the RIB key, calculating the social security number key, etc.
In short, a bunch of functions that I'd like to be able to use without having to…
public b as new baseric2
toto = b.alltrim("le texte ")
In short, my own toolbox of sorts
cordialement
Posted
Enthusiast


A module in Gambas is actually a static class.
To return to your example:
In the MTools module, you write:
Code
Public Function AllTrim(sToto As String) As String
Return Trim(sToto)
End and then you use it like this:
Code
Public Sub Main()
Dim s As String = " Hello world "
Print MTools.AllTrim(s)
End
Posted
Trainee

I would have preferred not to have to type `m.tools.` before each of my functions. Some utility functions, like those that calculate bank account numbers or validity dates with elements like the first day of the month, the last day, etc., are problematic. Currently, adding a ``
public b as new baseric
allows me to add a `b.` before each function call, but if I could have avoided that, it would have been much easier.
Thank you anyway
Posted
Regular


Eg module mytrim
Public sub _call(…) as string
What ever here
Return stuff
End
Then in your app you just
Public sub main()
Dim s as string = mytrim("stuff")
End
Only one entry point per module
You build up your toolbox one at a time.
You can then combine all your tools into a library that you can include into your projects.
Hope that helps a little
Last edit: by ercoupeflyer
Posted
Trainee

Posted
Enthusiast

you can set a keyborad shortshortcut to "p" or what ever you want .
Then just type p and hit tab and it will put "Module1" in it place then type "." and your function … or put the "." in the snippets
Last edit: by GrayGhost
Posted
Regular


This will allow you to just call your tools without and header information no more needed x.mytool formats.
Copy everything between the lines and paste them into a linux text file called mytools , chmod 777 mytools , then execute it : ./mytools to see the example work
The example is a gambas script file.
You can convert the script to a gambas project by entering : gbs3 –convert-script mytools /tmp/
You can then open the project in the ide at /tmp/mytools
go to each tool class and un-comment EXPORT at the top of the tool classes
The following is how to create your tool library.
To create the library go into the project properties and on the main page change the project type to library.
You can make an executable(at this point the library will be available for development purposes), and then an installation package for your distro, install it where ever your tools are needed
For development it will appear under ordinary when you go to properties, click on libraries, then the plus sign.
now in your new projects, under property click libraries and add it, your tools will be available to that app.
#!/usr/bin/env gbs3
' Gambas Script File Created 01/01/2026 21:07:57
'' this script contains the definition Of two sample tools
'' in practice you will define each class in the IDE
'' each one is the definition of one of your private tools
'' once your happy with your tools, then set the project as a library
'' build the library using the ide and create an installation package
'' install the package, now just use the library by including it into your projects
'' needing the tools you have created
class mytrim
export
'' example of a special trim I need for me
static Public Sub _call(source As String) As String
Return "Sample trimmer " & Trim(source)
End
End class
class SetQuotes
export
'' some other tool I want to define
static Public Sub _call(source As String) As String
Return Quote(Trim(source)) & ": more stuff"
End
End class
'' now I can just call the tools in my main application programs
Print mytrim(" this stuff ")
Print SetQuotes(" that stuff ")
Quit 0
Catch
Error "Script Error >";; error.text & "\n" & error.where
I hope this helps a little
Last edit: by ercoupeflyer
Posted
Trainee

I'll try it. Thanks and Happy New Year 26
Posted
Trainee

Thank you so much. It allows me to take code from Visual FoxPro and keep some programs without changing the code too much.
I'm liking Gambas 3 more and more. It's really powerful.
Posted
Regular


1 guest and 0 members have just viewed this.



