|
|
|
|
|
- Overview
- Declaration
- Examples
- Replace tabs with spaces
- Extension of the Color type
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 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 behaves in the same way as in classes or fields: it refers to the element currently being called..
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)
RETURN WL.This.Formate(ccUpCase)
- Procedures can be defined for a given field type using the <type champ>. For example, the following procedure can be used for a Button field:
PROCEDURE Bouton(x is Control <type control = typButton>)
It is possible to extend a procedure for a given field 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>
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)
Trace(sTexte.RemplaceTABParXEspace(4))
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
rLuminance = 1 - (0.299 * CouleurOrigine..Red + ...
0.587 * CouleurOrigine..Green + 0.114 * CouleurOrigine..Blue)/255
IF rLuminance < 0.5 THEN
nLuminosité = 12
ELSE
nLuminosité = 88
END
CouleurRetour = HSL(ColorHue(CouleurOrigine), ColorSaturation(CouleurOrigine), nLuminosité)
RETURN CouleurRetour
Call to the extension procedure:
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
MaCouleur = HTMLToRGB(tabCouleursHTML[Random(1, tabCouleursHTML..Count)] )
LIB_TestCouleurFond.CouleurFond = MaCouleur
LIB_TestCouleurFond.Couleur = MaCouleur.CouleurLisible()
Related Examples:
|
Training: ExtensionProcedures
[ + ] This example illustrates the use of the <extension> attribute, to add functionalities to WLanguage types.
|
This page is also available for…
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|