AYUDA EN LÍNEA
 WINDEVWEBDEV Y WINDEV MOBILE

Este contenido se ha traducido automáticamente.  Haga clic aquí  para ver la versión en inglés.
Ayuda / Editores / Editor de proyectos / Descripción del proyecto
  • Overview
  • Allowing nullable types
  • Functionalities associated with the activation of nullable types
  • Storing null values in a variable
  • Overview
  • Nullable simple type
  • Determining if an element is null
  • Getting the value of an element that allows null values
  • ?! operator
  • The?? (coalescence operator)
  • ??* operator
  • ??= operator
  • ??=* operator
  • Using null values in WLanguage functions
  • Special case: Serialize / Deserialize
  • Special case: prefix syntax
  • Using null values in procedures
  • Passing parameters by value
  • Return value
  • Operations available on variables that allow null values
  • Arithmetic operations
  • Comparison operations
  • Boolean operations
  • Conditions
WINDEV
WindowsLinuxJavaReportes y ConsultasCódigo de Usuario (UMC)
WEBDEV
WindowsLinuxPHPWEBDEV - Código Navegador
WINDEV Mobile
AndroidWidget Android iPhone/iPadIOS WidgetApple WatchMac Catalyst
Otros
Procedimientos almacenados
Overview
Starting with version 2024, you can allow nullable types in a WINDEV, WEBDEV or WINDEV Mobile project. Allowing nullable types in a project changes the way your project handles null values.
This help page presents the various functionalities associated with this option.

Allowing nullable types

To allow nullable types:
  1. Open the project description window. To do so, go to the "Proyecto" tab, "Proyecto" group, and click "Descripción".
  2. On the "Compilation" tab, check "Permitir tipos que admiten valores NULL".
  3. A message about the implications of enabling this option appears. Click "Enable".
  4. Validate the project description window.

Functionalities associated with the activation of nullable types

When the "Permitir tipos que admiten valores NULL" option is selected, a series of functionalities are enabled. These features change the way null values are handled:
Storing null values in a variable

Overview

When "Permitir tipos que admiten valores NULL" is enabled, there are certain criteria to assign Null constants to simple variable types. In this mode, simple types (int, string, date, etc.), called non-nullable types, do not allow null values.
Note For complex types (object, array, etc.), the behavior is unchanged: a complex type can be Null.
To use the Null constant with simple types, it is now necessary to use nullable simple types.

Nullable simple type

To define a nullable simple type, use one of the following syntaxes:
  • add a question mark ("?") after the name of the type.
    <Variable name> is <Type>?
    For example:
    n is int?
    s is Unicode string?
    n1 is 8-byte int?
  • use the <nullable> extension attribute.
    <Variable name> is <Type> <nullable>
    For example:
    n is int <nullable>
Remarks:
  • These syntaxes are only available if the "Permitir tipos que admiten valores NULL" option is enabled.
  • The default value of a nullable type is Null (and no longer the type's initial value).
  • Type inference is available for nullable types. For example:
    LET x = ProcedureReturnNullableInteger()
The following simple types can be nullable:
Determining if an element is null
When the "Permitir tipos que admiten valores NULL" option is enabled, the result of a comparison of an element with the Null constant changes, as follows:
  • a simple non-nullable type can be compared with the constant Null constant: the result always corresponds to False.
  • a simple nullable type can be compared with the constant Null constant: the result corresponds to True if its value is Null, False otherwise.
  • a complex type that allowed the value Null in previous versions (object, array, etc.) doesn't change its behavior: it can still be compared with the value Null.
Remarks:
  • HFSQL field If the data file and the field allow the value Null, it is now possible to know if an HFSQL field is Null by comparing it directly to the constant Null. Example:
    IF Queue.ItemWithNullValue = Null THEN
    	// Process ...
    END
  • Edit control field If the "Devolver NULL si está vacío" option is checked for the field, it is possible to find out whether an Edit control has returned Null.
Getting the value of an element that allows null values
When you enable the "Permitir tipos que admiten valores NULL" option, you need to check the code used to get the value of an element that allows null values.
Assigning the value of a nullable element to a nullable element is perfectly fine. The Null value is preserved, if necessary.
However, assigning a nullable element to a non-nullable element causes a compilation error. For example, the following lines of code are not allowed:
s is string = Queue.NullableItem
ou
Left(Queue.NullableItem)

You need to specify how Null values should be handled, directly in the line of code. To do this, the WLanguage provides several operators for accessing the values of elements that allow the Null value: ?!, ??, ??*, ??= and ??=*.

?! operator

If it is certain that the value will be non-null, you can use the ?! operator. The compilation error related to the use of a nullable element will not be displayed in this case.
Syntax:
nullable_element?!
Example:
IF Queue.ItemWithNull <> Null THEN
	s is string = Queue.ItemWithNull?!
END
Reminder: If the code is erroneous and the value is Null at runtime, an exception will be raised by the WLanguage security mechanism.
Note: In many cases, the?! operator is automatically applied to simplify code writing:
  • on elements that cannot be determined at compile time: query results, indirection, etc.
  • in certain conditional syntaxes. For example:
    IF Queue.ItemWithNull <> Null THEN
    	s is string = Queue.ItemWithNull	//?! operator automatically added
    END
Warning: automatic application of the?! operator does not dispense with handling the case of the Null value. Otherwise, an exception will be thrown at runtime.

The?? (coalescence operator)

The ?? operator allows you to get:
  • the value of a nullable element if this value is non-null,
  • a substitution value, if the nullable element is set to Null.
Syntax:
nullable_expression?? substitution_value
Example:
// The string will contain "N/A" if the item is Null
s is string = Queue.ItemWithNull?? "N/A"

??* operator

The ??* operator allows you to get:
  • the value of a nullable element if this value is non-null.
  • the default value if the type, if the nullable element is set to Null.
Syntax:
nullable_expression??*
Example:
// The string will be empty if the item is Null
s is string = Queue.ItemWithNull??*

??= operator

The ??= operator makes it possible to directly assign a substitution value to a nullable element that holds the Null value. This operator is only available for variables or items in data files.
Syntax:
nullable_element??= substitution_value
Example:
nullable_value is string?
nullable_value = ProcedureToBeExecuted()
nullable_value??= "N/A"
This is more concise than the code below:
nullable_value is string?
nullable_value = ProcedureToBeExecuted()
IF nullable_value = Null THEN
	nullable_value = "N/A"
END

??=* operator

The ??=* operator makes it possible to directly assign a default value to a nullable element that holds the Null value. This operator is only available for variables or items in data files.
Syntax:
nullable_element??=*
Example:
nullable_value is string?
	nullable_value = ProcedureToBeExecuted()
	nullable_value??=*
This is more concise than the code below:
nullable_value is string?
nullable_value = ProcedureToBeExecuted()
IF nullable_Value = Null THEN
	nullable_value??*
END
Using null values in WLanguage functions
To simplify debugging, Trace displays null values or nullable types:
  • if the value is Null, the "<Null>" string is displayed.
  • if the type is nullable but does not hold a Null value, the "<Nullable>" string is displayed next to the value.
The TypeVar and DataType functions return the base type.
Starting with version 2024 Update 2, you can use TypeIsNullable to determine if a type is nullable.
Note Many WLanguage functions allow Null values and work as before (TreeAdd, TreeModify, etc.).

Special case: Serialize / Deserialize

  • In a binary deserialization, nullable members are set to Null if they do not exist in the serialized buffer.
  • In an XML serialization, nullable members that hold the Null value are not serialized. During deserialization, members are set to Null if they do not exist in the XML content.
  • In a JSON serialization, nullable members that hold the Null value are not serialized. During deserialization, members are set to Null if they do not exist in the JSON content.

Special case: prefix syntax

The ?. operator makes it possible to use a WLanguage function directly on a nullable type, without using a variable.
Syntax:
result = nullable_element?. WLanguageFunction(...)
  • If nullable_element is not Null, the function is called.
  • If nullable_element is Null, the function is not called and the result is Null.
Example:
full_string_or_Null is string? = ...
first_ten_characters_or_Null is string? = full_string_or_Null?. Left s(10)
You can combine the ?. and ?? operators to get a simple result:
first_ten_characters is string = full_string_or_Null?. Left s(10)?? ""
Using null values in procedures

Passing parameters by value

Null or nullable values cannot be passed to a non-nullable parameter of a simple type, or to a non-typed non-nullable parameter.
To pass Null or a nullable value to a parameter, use the following syntaxes:
PROCEDURE ProcedureWithANullableParameter(parameter?)
PROCEDURE ProcedureWithANullableParameter(parameter is int?)
Note: Implicit conversions between simple types apply between the corresponding nullable types.

Return value

Similarly to parameters, you must specify that the return type is nullable if a procedure is to return Null or a nullable value.
The corresponding syntaxes are:
PROCEDURE ProcedureReturnsANullableValue ():?
PROCÉDURE ProcedureRenvoieUneValeurNullable (): integer?
Operations available on variables that allow null values

Arithmetic operations

Arithmetic and string operations are not allowed if one of the operands is Null.
Executing these operations triggers an exception.
A compilation error allows you to identify these cases.
Arithmetic operations:
  • + (addition and concatenation)
  • ++
  • +=
  • -
  • --
  • -=
  • *
  • *=
  • / (division and string concatenation for paths)
  • /=
  • %
  • ^

Comparison operations

The = (equal to) operator returns:
  • True if both values are Null;
  • False if one value is Null and the other is not.
The <> (not equal to) operator returns:
  • True if one value is Null and the other is not;
  • False if both values are Null.
Other comparisons raise an exception if one of the values is Null:
  • <
  • <=
  • >
  • >=
  • ~=
  • ~~
  • [=
  • [~
  • [~~
  • =]
  • ~]
  • ~~]
  • [=]
  • [~]
  • [~~]

Boolean operations

Boolean operations have a particular behavior with nullable Boolean values.
  • PAS
    • NOT Null = Null
    • NOT False = True
    • NOT True = False
  • OR
    NullFalseTrue
    NullNullNullTrue
    FalseNullFalseTrue
    TrueTrueTrueTrue
  • ET
    NullFalseTrue
    NullNullFalseNull
    FalseFalseFalseFalse
    TrueNullFalseTrue

Conditions

In a condition (IF, WHILE, etc.), the Null value is equivalent to False.
Versión mínima requerida
  • Versión 2024
Esta página también está disponible para…
Comentarios
Haga clic en [Agregar] para publicar un comentario

Última modificación: 27/03/2025

Señalar un error o enviar una sugerencia | Ayuda local