Back to Devexpress

NullValueAttribute Class

xpo-devexpress-dot-xpo-33b670c4.md

latest4.6 KB
Original Source

NullValueAttribute Class

Associates a constant with the null value of a property or a field of a simple type.

Namespace : DevExpress.Xpo

Assembly : DevExpress.Xpo.v25.2.dll

NuGet Package : DevExpress.Xpo

Declaration

csharp
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, Inherited = true)]
public sealed class NullValueAttribute :
    Attribute
vb
<AttributeUsage(AttributeTargets.Property Or AttributeTargets.Field, Inherited:=True)>
Public NotInheritable Class NullValueAttribute
    Inherits Attribute

Remarks

The NullValueAttribute is provided for compatibility with .NET Framework 1.x which doesn’t have support for nullable types. The NullValueAttribute allows null values to be stored in a database and displayed in a UI. Whenever XPO stores a property or a field value to a database or supplies it to a data-bound control, the value is compared with the constant associated with a null value. If they are equal, null (Nothing in VB) is returned for the property or field instead of the associated value. Similarly, when the property’s value is retrieved from the database or the data-bound control, it is compared with a null value. If these values are equal, the NullValueAttribute.Value is returned.

csharp
public class Example : XPObject {
    [NullValue(Int32.MinValue)]
    public int NullOnMinValueFieldInt {
     get { return fNullOnMinValueFieldInt; }
     set { SetPropertyValue(nameof(NullOnMinValueFieldInt), ref fNullOnMinValueFieldInt, value); }
 }
 int fNullOnMinValueFieldInt;

    [NullValue(0)]
    public int NullOnZeroFieldInt {
     get { return fNullOnZeroFieldInt; }
     set { SetPropertyValue(nameof(NullOnZeroFieldInt), ref fNullOnZeroFieldInt, value); }
 }
 int fNullOnZeroFieldInt;

    [NullValue("")]
    public string NullOnEmptyStringFieldString {
     get { return fNullOnEmptyStringFieldString; }
     set { SetPropertyValue(nameof(NullOnEmptyStringFieldString), ref fNullOnEmptyStringFieldString, value); }
 }
 string fNullOnEmptyStringFieldString;

    [NullValue("NULL")]
    public string NullOnNULLFieldString {
     get { return fNullOnNULLFieldString; }
     set { SetPropertyValue(nameof(NullOnNULLFieldString), ref fNullOnNULLFieldString, value); }
 }
 string fNullOnNULLFieldString;

}
vb
Public class Example : Inherits XPObject
    <NullValue(Int32.MinValue)> _
    Public Property NullOnMinValueFieldInt() As Int
        Get
            Return fNullOnMinValueFieldInt
        End Get
        Set(ByVal value as Int)
            SetPropertyValue(NameOf(NullOnMinValueFieldInt), fNullOnMinValueFieldInt, value)
        End Set
    End Property
    Private fNullOnMinValueFieldInt As Int

    <NullValue(0)> _
    Public Property NullOnZeroFieldInt() As Int
        Get
            Return fNullOnZeroFieldInt
        End Get
        Set(ByVal value as Int)
            SetPropertyValue(NameOf(NullOnZeroFieldInt), fNullOnZeroFieldInt, value)
        End Set
    End Property
    Private fNullOnZeroFieldInt As Int

    <NullValue("")> _
    Public Property NullOnEmptyStringFieldString() As String
        Get
            Return fNullOnEmptyStringFieldString
        End Get
        Set(ByVal value as String)
            SetPropertyValue(NameOf(NullOnEmptyStringFieldString), fNullOnEmptyStringFieldString, value)
        End Set
    End Property
    Private fNullOnEmptyStringFieldString As String

    <NullValue("NULL")> _
    Public Property NullOnNULLFieldString() As String
        Get
            Return fNullOnNULLFieldString
        End Get
        Set(ByVal value as String)
            SetPropertyValue(NameOf(NullOnNULLFieldString), fNullOnNULLFieldString, value)
        End Set
    End Property
    Private fNullOnNULLFieldString As String

End Class

Note that nullable types are supported by XPO under .NET Framework 2.0 and later.

Inheritance

Object Attribute NullValueAttribute

See Also

NullValueAttribute Members

Built-In Attributes

DevExpress.Xpo Namespace