IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Macros et VBA Excel Discussion :

Utilisation de l'interface IComparer


Sujet :

Macros et VBA Excel

  1. #1
    Membre habitué
    Homme Profil pro
    Ingénieur Informatique et Réseaux
    Inscrit en
    Avril 2011
    Messages
    232
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Isère (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur Informatique et Réseaux
    Secteur : Industrie

    Informations forums :
    Inscription : Avril 2011
    Messages : 232
    Points : 182
    Points
    182
    Par défaut Utilisation de l'interface IComparer
    Bonjour,
    Dans une macro, j'utilise des ArrayList qui contiennent des objets de la classe Match.
    J'aimerai utiliser la méthode Sort(IComparer) afin de trier comme je veux cette liste, mais voila l'interface IComparer est introuvable lors de l'appel dans MatchComparer (Implements).
    Comment puis-je trier ma liste facilement?

    Merci de votre aide.


    Voici mon code:

    Module de classe : Match
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    Option Explicit
     
    Private matchDate As Date
    Private matchLine As Long
     
    Public Property Let setMatchDate(matchDateNew As Date)
        matchDate = matchDateNew
    End Property
     
    Public Property Get getMatchDate() As Date
        getMatchDate = matchDate
    End Property
     
    Public Property Let setMatchLine(matchLineNew As Long)
        matchLine = matchLineNew
    End Property
     
    Public Property Get getMatchLine() As Long
        getMatchLine = matchLine
    End Property
     
    Public Property Get toString() As String
        toString = CStr(matchDate) + "//" + CStr(matchLine)
    End Property
    Module de classe : MatchComparer
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    Option Explicit
    Implements IComparer
     
    Private Function IComparer_Compare(match1 As match, match2 As match) As Long
        If match1.getMatchDate > match2.getMatchDate Then
            IComparer_Compare = -1
        End If
        If match1.getMatchDate < match2.getMatchDate Then
            IComparer_Compare = 1
        End If
        If match1.getMatchDate = match2.getMatchDate Then
            IComparer_Compare = 0
        End If
    End Function
    Module : Module2
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    Option Explicit
     
    Sub test()
        Dim listeMatchs As Object
        Set listeMatchs = CreateObject("System.Collections.ArrayList")
     
        Dim match1 As match
        Dim match2 As match
        Dim match3 As match
     
     
        Set match1 = New match
        match1.setMatchDate = CDate("30/12/2014 11:00")
        match1.setMatchLine = 1
        Set match2 = New match
        match2.setMatchDate = CDate("29/12/2014 11:00")
        match2.setMatchLine = 2
        Set match3 = New match
        match3.setMatchDate = CDate("31/12/2014 11:00")
        match3.setMatchLine = 3
     
        listeMatchs.Add match1
        listeMatchs.Add match2
        listeMatchs.Add match3
     
     
        listeMatchs.Sort (New MatchComparer)
     
        Dim element As match
        For Each element In listeMatchs
            MsgBox element.toString
        Next element
    End Sub

  2. #2
    Invité
    Invité(e)
    Par défaut
    Salut,

    Sublime ta méthode.

    Pour ton problème, créé une classe appelé IComparer avec une fonction vide comme suit:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    Function Compare(match1 As Match, match2 As Match) As Long
    End Function

  3. #3
    Membre habitué
    Homme Profil pro
    Ingénieur Informatique et Réseaux
    Inscrit en
    Avril 2011
    Messages
    232
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Isère (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur Informatique et Réseaux
    Secteur : Industrie

    Informations forums :
    Inscription : Avril 2011
    Messages : 232
    Points : 182
    Points
    182
    Par défaut
    Bonjour,
    Merci pour ta réponse mais créer une interface IComparer ne fonctionnera pas avec la méthode Sort de l'ArrayList qui prend en paramètre un IComparer (celui de System.Collections).

  4. #4
    Invité
    Invité(e)
    Par défaut
    Ah oui, exact.


    ---------------
    EDIT:
    Remplace
    listeMatchs.Sort New MatchComparer
    par
    listeMatchs.Sort_2 New MatchComparer

    et avec une signature dans MatchComparer comme suit
    Private Function IComparer_Compare(ByVal x As Variant, ByVal y As Variant) As Long
    plutôt que
    Private Function IComparer_Compare(match1 As match, match2 As match) As Long
    Dernière modification par Invité ; 01/01/2015 à 19h16.

  5. #5
    Membre habitué
    Homme Profil pro
    Ingénieur Informatique et Réseaux
    Inscrit en
    Avril 2011
    Messages
    232
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Isère (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur Informatique et Réseaux
    Secteur : Industrie

    Informations forums :
    Inscription : Avril 2011
    Messages : 232
    Points : 182
    Points
    182
    Par défaut
    Ceci ne fonctionne pas:
    Erreur d’exécution '5':
    Argument ou appel de procédure incorrect


    Mais que veut dire le "_2" sur la méthode Sort? Je ne connais pas cette syntaxe.

Discussions similaires

  1. Réponses: 3
    Dernier message: 22/03/2007, 12h06
  2. Réponses: 5
    Dernier message: 13/02/2007, 09h53
  3. Utilisation de sous interface
    Par AnthonyL44 dans le forum SWT/JFace
    Réponses: 5
    Dernier message: 22/08/2006, 17h18
  4. [EJB] Utiliser seulement les interfaces locales avec les ejb
    Par clement42 dans le forum Java EE
    Réponses: 1
    Dernier message: 06/01/2006, 13h12
  5. Composants à utiliser pour une interface graphique Java
    Par nicolas.pied dans le forum Composants
    Réponses: 4
    Dernier message: 28/11/2005, 21h27

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo