module of personal function

Post

Posted
Rating:
#1 (In Topic #1988)
Avatar
Trainee
LEISSLER is in the usergroup ‘Trainee’
Hello everyone and Merry Christmas!

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:

Code (gambas)

Static Public Function alltrim(toto As String) As String

  Return Trim(toto)

End

and in my code I could do

Code (gambas)

implements baseric2 Public Sub main() Print alttrim("bonjour") End
implements baseric2

Public Sub main()

  Print alttrim("bonjour")

End
 

How can I do the same thing with Gambas3? And is it even possible?

Regards


 
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Enthusiast
Gianluigi is in the usergroup ‘Enthusiast’
Gianluigi is in the usergroup ‘GambOS Contributor’
The Trim function already exists, why do you want to duplicate it?
In Gambas, just type this:

Code

  Dim s As String = "  Hello world "
  Print Quote(Trim(s))

 :goodbye:
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Trainee
LEISSLER is in the usergroup ‘Trainee’
I chose this example to keep things simple.

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
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Enthusiast
Gianluigi is in the usergroup ‘Enthusiast’
Gianluigi is in the usergroup ‘GambOS Contributor’
Sorry, as usual, I misunderstood.  :$
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

 :goodbye:
Online now: No Back to the top

Post

Posted
Rating:
#5
Avatar
Trainee
LEISSLER is in the usergroup ‘Trainee’
I suppose that's not possible then!

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
Online now: No Back to the top

Post

Posted
Rating:
#6
Avatar
Regular
ercoupeflyer is in the usergroup ‘Regular’
ercoupeflyer is in the usergroup ‘Blogger’
If you define the special function _call then you can create classes that will act as a called function. You can include as many supporting functions as required in the class/ module file
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

Online now: No Back to the top

Post

Posted
Rating:
#7
Avatar
Trainee
LEISSLER is in the usergroup ‘Trainee’
This code doesn't work for me. Can you be a little more specific?
Online now: No Back to the top

Post

Posted
Rating:
#8
Avatar
Enthusiast
GrayGhost is in the usergroup ‘Enthusiast’
If you are trying to save typing  Module1.myfunction

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

Screenshot from 2026-01-01 11-07-14.png

Last edit: by GrayGhost

Online now: No Back to the top

Post

Posted
Rating:
#9
Avatar
Regular
ercoupeflyer is in the usergroup ‘Regular’
ercoupeflyer is in the usergroup ‘Blogger’
Ok, I can be more specific with an example
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

Online now: No Back to the top

Post

Posted
Rating:
#10
Avatar
Trainee
LEISSLER is in the usergroup ‘Trainee’
Wow, that looks very interesting.

I'll try it. Thanks and Happy New Year 26
Online now: No Back to the top

Post

Posted
Rating:
Item has a rating of 5 (Liked by gbWilly)
#11
Avatar
Trainee
LEISSLER is in the usergroup ‘Trainee’
Great, it works perfectly.

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.
Online now: No Back to the top

Post

Posted
Rating:
Item has a rating of 5 (Liked by gbWilly)
#12
Avatar
Regular
ercoupeflyer is in the usergroup ‘Regular’
ercoupeflyer is in the usergroup ‘Blogger’
Happy that helped you! ;)
Online now: No Back to the top
1 guest and 0 members have just viewed this.