TimeSpanMinutesConverter Class

Note: This class is new in the .NET Framework version 2.0.

Converts a time span expressed in minutes.

Namespace: System.Configuration
Assembly: System.Configuration (in system.configuration.dll)

'Declaration
Public Class TimeSpanMinutesConverter
	Inherits ConfigurationConverterBase
'Usage
Dim instance As TimeSpanMinutesConverter

public class TimeSpanMinutesConverter extends ConfigurationConverterBase
public class TimeSpanMinutesConverter extends ConfigurationConverterBase

This type, like all the other configuration converter types, converts strings found in the configuration file to and from the related strongly typed properties.

In particular, the TimeSpanMinutesConverter converts String minutes, assigned to a configuration property, to TimeSpan minutes and vice versa.

The TimeSpanMinutesConverter persists values of type long representing a number of minutes.

The following code example shows how to define a custom TimeSpanMinutesConverter type.

Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Configuration
Imports System.Globalization
Imports System.ComponentModel




NotInheritable Public Class CustomTimeSpanMinutesConverter
    Inherits ConfigurationConverterBase
    
    Friend Function ValidateType(ByVal value As Object, _
    ByVal expected As Type) As Boolean
        Dim result As Boolean

        If Not (value Is Nothing) _
        AndAlso value.ToString() <> expected.ToString() Then
            result = False
        Else
            result = True
        End If
        Return result

    End Function 'ValidateType
    
    
    Public Overrides Function CanConvertTo( _
    ByVal ctx As ITypeDescriptorContext, _
    ByVal type As Type) As Boolean
        Return (type.ToString() = GetType(String).ToString())

    End Function 'CanConvertTo
    
    Public Overrides Function CanConvertFrom( _
    ByVal ctx As ITypeDescriptorContext, _
    ByVal type As Type) As Boolean
        Return (type.ToString() = GetType(String).ToString())

    End Function 'CanConvertFrom
    
    Public Overrides Function ConvertTo( _
    ByVal ctx As ITypeDescriptorContext, _
    ByVal ci As CultureInfo, ByVal value As Object, _
    ByVal type As Type) As Object
        ValidateType(value, GetType(TimeSpan))

        Dim data As Long = _
        Fix(CType(value, TimeSpan).TotalMinutes)

        Return data.ToString(CultureInfo.InvariantCulture)

    End Function 'ConvertTo
    
    Public Overrides Function ConvertFrom( _
    ByVal ctx As ITypeDescriptorContext, _
    ByVal ci As CultureInfo, ByVal data As Object) As Object

        Dim min As Long = _
        Long.Parse(CStr(data), CultureInfo.InvariantCulture)

        Return TimeSpan.FromMinutes(System.Convert.ToDouble(min))

    End Function 'ConvertFrom

End Class 'CustomTimeSpanMinutesConverter 

The following is a configuration excerpt used by the previous example.

<configuration>

  <configSections>
    <section name="CustomSection"
      type="Samples.AspNet.CustomSection,
      ConfigurationConverters,
      Version=1.0.0.0, Culture=neutral,
      PublicKeyToken=null"
      allowDefinition="Everywhere"
      allowExeDefinition="MachineToApplication"
      restartOnExternalChanges="true" />
  </configSections>

  <CustomSection fileName="default.txt" maxIdleTime="90" 
    timeDelay="infinite" cdStr="str0, str1" permission="Read" 
    maxUsers="Infinite"/>

</configuration>

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

Community Additions

ADD
Show: