

pricerc wrote:If we're upgrading custom form data that has dates, would there be merit in converting them to ISO format from AU format ?
pricerc wrote:Also,
is there a particular reason why CustomFieldValue.GetValue<T> returns a System.Object and not a <T> ?
Public Function GetValue(Of T)(Name As String, DefaultValue As T) As T
Dim customFieldValue As CustomFieldValue = ItemFromSettingName(Name)
If customFieldValue Is Nothing Then
Return DefaultValue
Else
Return customFieldValue.GetValue(DefaultValue)
End If
End Function

Mike.Sheen wrote:No reason I guess other than nobody bothered to cast the result to T.
return (T) Convert.ChangeType(textValue, typeof(T));
<Extension()>
Public Function GetValueT(this As CustomFields.CustomFieldValue, ByVal DefaultValue As DateTime) As DateTime
If String.IsNullOrWhiteSpace(this.Contents) Then
Return DefaultValue
End If
Dim result As DateTime
If DateTime.TryParseExact(this.Contents, "o", Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, result) Then
' preferred, new default format
Return result
ElseIf DateTime.TryParseExact(this.Contents, "yyyy-MM-dd", Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, result) Then
' truncated ISO format
Return result
ElseIf DateTime.TryParseExact(this.Contents, "dd/MM/yyyy", Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, result) Then
' legacy Jiwa format
Return result
ElseIf DateTime.TryParse(this.Contents, result) Then
' generic format
Return result
End If
Return DefaultValue
End Function
<Extension()>
Public Function GetValueT(this As CustomFields.CustomFieldValue, ByVal DefaultValue As Integer) As Integer
If String.IsNullOrWhiteSpace(this.Contents) Then
Return DefaultValue
End If
Try
' because we're in VB, although Int32.TryParse should work as well
Return CInt(this.Contents.Trim())
Catch ex As Exception
Return DefaultValue
End Try
End Function
<Extension()>
Public Function GetValueT(this As CustomFields.CustomFieldValue, ByVal DefaultValue As Double) As Double
If String.IsNullOrWhiteSpace(this.Contents) Then
Return DefaultValue
End If
Try
' because we're in VB, although Double.TryParse should work as well
Return CDbl(this.Contents.Trim())
Catch ex As Exception
Return DefaultValue
End Try
End Function
<Extension()>
Public Function GetValueT(this As CustomFields.CustomFieldValue, ByVal DefaultValue As Decimal) As Decimal
If String.IsNullOrWhiteSpace(this.Contents) Then
Return DefaultValue
End If
Try
' because we're in VB, although Decimal.TryParse should work as well
Return CDec(this.Contents.Trim())
Catch ex As Exception
Return DefaultValue
End Try
End Function
<Extension()>
Public Function GetValueT(this As CustomFields.CustomFieldValue, ByVal DefaultValue As Boolean) As Boolean
If String.IsNullOrWhiteSpace(this.Contents) Then
Return DefaultValue
End If
' Even VB doesn't handle things like 'yes' and 'no' as boolean values.
' So because I'm interested in replacing legacy textbox or combobox yes/no fields with booleans,
' I'm going to do my own parsing.
If {"true", "yes", "y", "1", "-1"}.Contains(this.Contents.Trim().ToLowerInvariant()) Then
Return True
End If
If {"false", "no", "n", "0"}.Contains(this.Contents.Trim().ToLowerInvariant()) Then
Return False
End If
' could arguably include a test here for any non-zero integer to get a VB 'True'.
' if we get here, the value doesn't make sense.
Return DefaultValue
End Function

Dim x as Boolean = customFieldValue.CustomFieldValueCollection.GetValue(Of Boolean)(customFieldValue.CustomField.PluginCustomField.Name, false)
Mike.Sheen wrote:You could also use the existing GetValue method of the CustomFieldValueCollection.
- Code: Select all
Dim x as Boolean = customFieldValue.CustomFieldValueCollection.GetValue(Of Boolean)(customFieldValue.CustomField.PluginCustomField.Name, false)
For Each dbv As CustomFieldValue In debtor.CustomFieldValues
Select Case dbv.Name
Case "Foo"
foo = dbv.Contents
Case "Bar"
bar = dbv.Contents.ToUpperInvariant()
Case "When"
when = dbv.GetValueT(#01/01/2000#)
Case "meh"
meh = dbv.Contents
...
End Select
Next

Return to Technical and or Programming
Users browsing this forum: No registered users and 3 guests