Xafari Object Formatter. Examples
Example 1
Format Double type object, remains 2 characters after the point.
Example 2
Format DateTime value:
- c#
- VB
//string for specifying format
private string dateFormat;
public string DateFormat
{
get
{
return dateFormat;
}
set
{
SetPropertyValue("DateFormat", ref dateFormat, value);
}
}
//processed date
private DateTime dateData;
public DateTime DateData
{
get
{
return dateData;
}
set
{
SetPropertyValue("DateData", ref dateData, value);
}
}
//the resulting function, which returns the formatted data
public string DataFormatter
{
get
{
if (String.IsNullOrEmpty(DateFormat))
return ForResultFormatter("{0:DateData}");
else
return ForResultFormatter(String.Format("{{0:DateData, {0}}}", DateFormat));
}
}
//the format function
public string ForResultFormatter(string formatString)
{
string formatData = String.Empty;
try
{
XafariObjectFormatter.Initialize();
formatData = XafariObjectFormatter.Format(formatString, this);
return formatData;
}
catch
{
return null;
}
}
'string for specifying format
Private dateFormat As String
Public Property DateFormat As String
Get
Return dateFormat
End Get
Set
SetPropertyValue("DateFormat", dateFormat, value)
End Set
End Property
'processed date
Private dateData As DateTime
Public Property DateData As DateTime
Get
Return dateData
End Get
Set
SetPropertyValue("DateData", dateData, value)
End Set
End Property
'the resulting function, which returns the formatted data
Public ReadOnly Property DataFormatter As String
Get
If [String].IsNullOrEmpty(DateFormat) Then
Return ForResultFormatter("{0:DateData}")
Else
Return ForResultFormatter([String].Format("{{0:DateData, {0}}}", DateFormat))
End If
End Get
End Property
'the format function
Public Function ForResultFormatter(ByVal formatString As String) As String
Dim formatData As String = [String].Empty
Try
XafariObjectFormatter.Initialize()
formatData = XafariObjectFormatter.Format(formatString, Me)
Return formatData
Catch
Return Nothing
End Try
End Function
Example 3
In the collection, to find the value by key and to convert in accordance with the specified format.
- c#
- VB
using System.Collections.Generic;
public void XafariObjectFormatTest2()
{
IDictionary<string, int> dictionary = new Dictionary<string, int> { { "Key1", 1 }, { "Key2", 2 }, { "Key3", 3 } };
var actual = XafariObjectFormatter.Format("{0:[Key2],000}", dictionary);
// actual = "002"
}
Imports System.Collections.Generic
Public Sub XafariObjectFormatTest2()
Dim dictionary As IDictionary(Of String, Integer) = New Dictionary(Of String, Integer)() With { With {"Key1", 1}, With {"Key2", 2}, With {"Key3", 3}}
Dim actual = XafariObjectFormatter.Format("{0:[Key2],000}", dictionary)
' actual = "002"
End Sub
Example 4
Using the Expressions with the condition to obtain the value to be formatted:
- c#
- VB
public void XafariObjectFormatTest4()
{
int value = 12;
var actual = XafariObjectFormatter.Format("Value is {0:\"Iif(This>9,'two-digit','one-digit')\"}", value);
// actual = ""Value is two-digit"
}
Public Sub XafariObjectFormatTest4()
Dim value As Integer = 12
Dim actual = XafariObjectFormatter.Format("Value is {0:" + Chr(34) + "Iif(This>9,'two-digit','one-digit')" + Chr(34) + "}", value)
' actual = ""Value is two-digit"
End Sub
Note
You can view the number of examples in the Xafari.FeatureCenter|Formatter|XafariFormatterDateObject.cs file of the Feature Center demo installed with Xafari. By default, the Feature Center application is installed in %PUBLIC%\Documents\Xafari Framework vXX.X.XXXX Demos\Feature Center.