DateTime ToString Converter C#到vb.net

时间:2015-03-11 05:07:39

标签: c# vb.net

我已将此代码转换为VB.net,如下所示

public class DateTimeToStringConverter: IValueConverter
{
    public object Convert(object value, Type type, object parameter, string language)
    {
        string result = "";

        if (value is DateTime)
        {
            DateTime theDate = (DateTime)value;
            result = string.Format("{0:d}", theDate);
        }
        else
        {
            DateTime theDate = DateTime.Now;
            result = string.Format("{0:d}", theDate);
        }

        return result;
    }

    public object ConvertBack(object value, Type type, object parameter, string language)
    {
        return System.Convert.ToDateTime(value);
    }
}

转换代码

我得到一个错误2 'DateTimeToStringConverter'必须实现'Function Convert(value As Object,targetType As System.Type,parameter As Object,language As String)As Object'for interface'Windows.UI.Xaml。 Data.IValueConverter'

        Public Function Convert(ByVal value As Object, ByVal type As Type, ByVal parameter As Object, ByVal language As String) As Object
        Dim result As String = ""

        If TypeOf value Is Date Then
            Dim theDate As Date = DirectCast(value, Date)
            result = String.Format("{0:d}", theDate)
        Else
            Dim theDate As Date = Date.Now
            result = String.Format("{0:d}", theDate)
        End If

        Return result
    End Function

    Public Function ConvertBack(ByVal value As Object, ByVal type As Type, ByVal parameter As Object, ByVal language As String) As Object
        Return System.Convert.ToDateTime(value)
    End Function

任何人都可以帮助我,在此先感谢,我很感激

1 个答案:

答案 0 :(得分:1)

最有可能implements关键字是解决方案。以这种方式更改代码:

Public Function Convert(ByVal value As Object, ByVal type As Type, ByVal parameter As Object, ByVal language As String) As Object implements IValueConverter.Convert
        Dim result As String = ""

        If TypeOf value Is Date Then
            Dim theDate As Date = DirectCast(value, Date)
            result = String.Format("{0:d}", theDate)
        Else
            Dim theDate As Date = Date.Now
            result = String.Format("{0:d}", theDate)
        End If

        Return result
    End Function