ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

Help / WLanguage / WLanguage syntax / WLanguage procedures
  • Overview
  • Declaration
  • Example
  • 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:
PROCEDURE MyCustomProcedure(s is string) <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.
  • New in version 2024
    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 Format(x is string): string <extension>
    Trace(This) // equivalent to Trace(x)
    RETURN WL.This.Format(ccUpCase) // Calls the standard Format function
Example

Replace tabs with spaces

Procedure to replace tabs with spaces:
PROCEDURE ReplaceTABWithXSpace(s is string, n is int) <extension>: string
RETURN s.Replace(TAB, RepeatString(" ", n))
Call to the extension procedure:
sText is string = TAB + "XXX"
Trace(sText) // <TAB>XXX
Trace(sText.ReplaceTABWithXSpace(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 ReadableTextColor(SourceColor is Color) <extension>: Color
 
nLightness is int
rLuminance is real
ColorResult is Color
 
 
// Calculate the luminance of the color passed as parameter
rLuminance = 1 - (0.299 * SourceColor..Red + ...
0.587 * SourceColor..Green + 0.114 * SourceColor..Blue)/255
 
// According to the luminance, the text color must have a low or high lightness
IF rLuminance < 0.5 THEN
// Low luminosity
nLightness = 12
ELSE
// High luminosity
nLightness = 88
END
 
// The returned color keeps the shade and the saturation
ColorResult = HSL(ColorHue(SourceColor), ...
ColorSaturation(SourceColor), nLightness)
 
RETURN ColorResult
Call to the extension procedure:
// Array of colors
arrHTMLColors is array of strings
arrHTMLColors.Add("#F48FB1")
arrHTMLColors.Add("#C2185B")
arrHTMLColors.Add("#64B5F6")
arrHTMLColors.Add("#1565C0")
arrHTMLColors.Add("#81C784")
arrHTMLColors.Add("#2E7D32")
 
MyColor is Color
 
// Gets a random color
MyColor = HTMLToRGB(arrHTMLColors[Random(1, arrHTMLColors.Count)])
 
// Sets the initial color as background color
STC_BackgroundColorTest.BackgroundColor = MyColor
 
// Sets an easily readable color as text color
STC_BackgroundColorTest.Color = MyColor.ReadableTextColor()
Related Examples:
ExtensionProcedures Training (WINDEV): 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: 09/05/2023

Send a report | Local help