Back to Devexpress

ValueConverter.ConvertFromStorageType(Object) Method

xpo-devexpress-dot-xpo-dot-metadata-dot-valueconverter-dot-convertfromstoragetype-x28-system-dot-object-x29.md

latest3.4 KB
Original Source

ValueConverter.ConvertFromStorageType(Object) Method

When overridden in a derived class, converts the value which is stored in a data store to the persistent property’s value.

Namespace : DevExpress.Xpo.Metadata

Assembly : DevExpress.Xpo.v25.2.dll

NuGet Package : DevExpress.Xpo

Declaration

csharp
public abstract object ConvertFromStorageType(
    object value
)
vb
Public MustOverride Function ConvertFromStorageType(
    value As Object
) As Object

Parameters

NameTypeDescription
valueObject

An object which represents the value to convert.

|

Returns

TypeDescription
Object

An object which represents the converted value.

|

Example

The following code example demonstrates how to store your boolean data in the database using the “T” string for true and “F” for false by defining a new ValueConverter descendant. You may need this converter when using an existing database in which boolean properties are stored such form.

csharp
//...       
[Size(1), ValueConverter(typeof(BooleanToStringValueConverter))]
public bool Answer {
    get { return GetPropertyValue<bool>(nameof(Answer)); }
    set { SetPropertyValue<bool>(nameof(Answer), value); }
}

//...
public class BooleanToStringValueConverter : ValueConverter {
    public override object ConvertFromStorageType(object value) {
        return Convert.ToString(value) == "T";
    }
    public override object ConvertToStorageType(object value) {
        return Convert.ToBoolean(value) ? "T" : "F";
    }
    public override System.Type StorageType {
        get { return typeof(string); }
    }
}
vb
'...       
<Size(1), ValueConverter(GetType(BooleanToStringValueConverter))> _
Public Property Answer() As Boolean
    Get
        Return GetPropertyValue(Of Boolean)(NameOf(Answer))
    End Get
    Set(ByVal value As Boolean)
        SetPropertyValue(Of Boolean)(NameOf(Answer), value)
    End Set
End Property

'...
Public Class BooleanToStringValueConverter
    Inherits ValueConverter
    Public Overrides Function ConvertFromStorageType(ByVal value As Object) As Object
        Return Convert.ToString(value) = "T"
    End Function
    Public Overrides Function ConvertToStorageType(ByVal value As Object) As Object
        Return If(Convert.ToBoolean(value), "T", "F")
    End Function
    Public Overrides ReadOnly Property StorageType() As System.Type
        Get
            Return GetType(String)
        End Get
    End Property
End Class

See Also

Value Converters

ValueConverter Class

ValueConverter Members

DevExpress.Xpo.Metadata Namespace