Implémenter IValueConverter en VB.Net (VB2008 Express) ?

Résolu
Fabasia Messages postés 45 Date d'inscription mercredi 26 mars 2008 Statut Membre Dernière intervention 17 août 2012 - 27 mai 2011 à 15:25
Fabasia Messages postés 45 Date d'inscription mercredi 26 mars 2008 Statut Membre Dernière intervention 17 août 2012 - 27 mai 2011 à 22:22
Je n'arrive tjs pas à implémenter IValueConverter.
En cherchant sur le web j'ai trouvé :

............
Hope someone can help me with the following... I am trying to implement IValueConverter, actually I "translated" some c# code to VB.net

This is the c# code:
Code:
public class MyValueConverter : IValueConverter
{
#region IValueConverter Members

object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if ((int)value > 0)
return 1;
else if ((int)value == 0)
return 0;
else
return -1;
}

object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}

#endregion
}


And this is my code:
Code:

Public Class MyValueConverter
Implements IValueConverter
#Region "IValueConverter Members"
Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As Globalization.CultureInfo) As Object
If (CInt(value) > 0) Then
Convert = 1
ElseIf CInt(value) = 1 Then
Convert = 0
Else
Convert = -1
End If
End Function
Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As Globalization.CultureInfo) As Object
Throw New NotImplementedException()
End Function
#End Region
End Class

It won't compile as it says i must implement Convert and ConvertBack... I am having a bad day, so probably there is something very obvious i am not seeing...

Thank you in advance for your help,
............

Apparemment, la personne a trouvé la solution, puisqu'elle répond :

............
ok... i was having a bad day... keyword implements at the end of the function declaration... that's what i was missing! problem solved.
............

J'ai essayé en vain déclarer Implement à différentes places, mais rien n'y fait !

SVP une explication pour m'aider. Merci

2 réponses

NHenry Messages postés 15112 Date d'inscription vendredi 14 mars 2003 Statut Modérateur Dernière intervention 13 avril 2024 159
27 mai 2011 à 20:35
Bonjour,

Tu as oublié la clause Implements des fonctions.

Pour faire simple, quand tu appuie sur la touche "Entrée" à la fin de la ligne de ton Implements, il te génère automatiquement les fonctions à implémenter. Tu verras les lignes à utiliser.

Public Function MaFonction (...) As Type Implements Interface.Fonction

3
Fabasia Messages postés 45 Date d'inscription mercredi 26 mars 2008 Statut Membre Dernière intervention 17 août 2012
27 mai 2011 à 22:22
En plus je n'avais pas ajouter la référence .NET PresentationFrameWork.

Comme ça ?


Imports System.Windows.Data

Public Class DateToStringConverter

    Implements IValueConverter

      Public Function Convert(ByVal value As Object, _
        ByVal targetType As Type, ByVal parameter As Object, _
        ByVal culture As System.Globalization.CultureInfo) As Object _
        Implements IValueConverter.Convert

        ' ...........
        
    End Function

        Public Function ConvertBack(ByVal value As Object, _
        ByVal targetType As Type, ByVal parameter As Object, _
        ByVal culture As System.Globalization.CultureInfo) As Object _
        Implements IValueConverter.ConvertBack

        Throw New NotImplementedException

    End Function

End Class




Merci !
0
Rejoignez-nous