ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

This content has been translated automatically.  Click here  to view the French version.
Help / WLanguage / WLanguage syntax / WLanguage procedures
  • Overview
  • Declaration
  • Examples
  • Replace tabs with spaces
  • Extension of the Color type
WINDEV
WindowsLinuxUniversal Windows 10 AppJavaReports and QueriesUser code (UMC)
WEBDEV
WindowsLinuxPHPWEBDEV - Browser code
WINDEV Mobile
AndroidAndroid Widget iPhone/iPadIOS WidgetApple WatchMac CatalystUniversal Windows 10 App
Others
Stored procedures
Overview
By using extension procedures, you can add your own WLanguage procedures to different types of variables (string, date, color etc.). These procedures can then be called as native WLanguage functions. The basic WLanguage type is therefore "extended".
To indicate that a procedure is an extension procedure, the following conditions must be met:
  • the first parameter uses the type that must be "extended",
  • the prototype of the procedure has the <extension> attribute.
Declaration
Declaration of an extension procedure of the String type:
PROCÉDURE MaProcédurePerso(s est une chaîne) <extension>
Remarks:
  • The extension procedures appear in the code completion suggestions of the extended type.
  • Extension procedures cannot be used with types such as Boolean, Integer, Real, Numeric, etc. (any type used to handle numeric values).
  • Only WLanguage types can be extended.
  • Only global procedures can have the <extension> attribute.
  • If the extension procedure overrides a WLanguage function, the standard syntax of that function will also be overridden. For example, creating a "Format" extension procedure for the String type will override both the <String>.Format and StringFormat functions.
  • You can chain the calls to extension procedures.
  • The 'This' keyword has the same behavior as in classes or controls: it refers to the current object.
    In an extension procedure, the 'This' keyword refers to the first parameter.
    Therefore, you can use 'This' preceded by 'WL' to refer to standard WLanguage functions and avoid calls to an overloaded function.
    Example:
    PROCEDURE Formate(x is string): string <extension>
    Trace(This) // équivalent à Trace(x)
    RETURN WL.This.Formate(ccUpCase) // Appel le Formate standard
  • Procedures can be defined for a given control type using the attribute . For example, the following Procedure can be used for an Button control:
    PROCEDURE Bouton(x is Control <type control = typButton>)

    It is possible to extend an Procedure for a given control type using the following syntax
    PROCÉDURE MaProcédureChampExtension(x est un champ <type champ = xxx>)<extension>

    Example:
    PROCEDURE ExtensionBouton(x is Control <type control = typButton>) <extension>
Examples

Replace tabs with spaces

Procedure to replace tabs with spaces:
PROCEDURE RemplaceTABParXEspace(s is string, n is int) <extension>: string
RETURN s.Remplace(TAB, RepeatString(" ", n))
Call to the extension procedure:
sTexte is string = TAB + "XXX"
Trace(sTexte) // <TAB>XXX
Trace(sTexte.RemplaceTABParXEspace(4)) //     XXX

Extension of the Color type

Extension procedure of the Color type, used to get a readable text color on a given background color.
PROCEDURE CouleurLisible(CouleurOrigine is Color) <extension>: Color
nLuminosité is int
rLuminance is real
CouleurRetour is Color

// Calcul de la luminance de la couleur passée en paramètre
rLuminance = 1 - (0.299 * CouleurOrigine..Red + ...
		0.587 * CouleurOrigine..Green + 0.114 * CouleurOrigine..Blue)/255
// En fonction de la luminance, la couleur d'écriture doit avoir une faible ou une forte luminosité
IF rLuminance < 0.5 THEN
	// Faible luminosité
	nLuminosité = 12
ELSE
	// Forte luminosité
	nLuminosité = 88
END
// La couleur renvoyée conserve la tonalité et la saturation
CouleurRetour = HSL(ColorHue(CouleurOrigine), ColorSaturation(CouleurOrigine), nLuminosité)

RETURN CouleurRetour
Call to the extension procedure:
// Tableau de couleurs
tabCouleursHTML is array of strings
tabCouleursHTML.Ajoute("#F48FB1")
tabCouleursHTML.Ajoute("#C2185B")
tabCouleursHTML.Ajoute("#64B5F6")
tabCouleursHTML.Ajoute("#1565C0")
tabCouleursHTML.Ajoute("#81C784")
tabCouleursHTML.Ajoute("#2E7D32")

MaCouleur is Color
// Récupère une couleur aléatoire
MaCouleur = HTMLToRGB(tabCouleursHTML[Random(1, tabCouleursHTML..Count)] )
// Applique la couleur initiale en couleur de fond
LIB_TestCouleurFond.CouleurFond = MaCouleur
// Applique une couleur lisible en couleur de texte
LIB_TestCouleurFond.Couleur = MaCouleur.CouleurLisible()
Related Examples:
ExtensionProcedures Training: ExtensionProcedures
[ + ] This example illustrates the use of the <extension> attribute, to add functionalities to WLanguage types.
Minimum version required
  • Version 28
This page is also available for…
Comments
Click [Add] to post a comment

Last update: 05/27/2024

Send a report | Local help