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 :

getFirst / getNext element d'une classe itérable


Sujet :

Macros et VBA Excel

  1. #1
    Nouveau membre du Club
    Homme Profil pro
    Ingénieur aéronautique
    Inscrit en
    Octobre 2018
    Messages
    216
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Ingénieur aéronautique

    Informations forums :
    Inscription : Octobre 2018
    Messages : 216
    Points : 30
    Points
    30
    Par défaut getFirst / getNext element d'une classe itérable
    Bonjour,

    Est-ce possible de définir une fonction getNext qui renverrait le field suivant définit dans fields ?

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    Dim fields As New clsFields
     
    fields.add ...
    fiedls.add ...
    ...
     
    Dim field as clsField: set field = fields.getFirst 'OK avec pFields(index:=1) où pFields serait la variable itérée dans clsFields
     
    Set field = field.getNext 'je pourrais bien sûr définir getNext dans clsFields (Set field = fields.getNext) et unitilisé par exemple un indice statique dans la méthode, mais ça me convient peu..
    ...
    où clsFields est une classe collection itérable (c'est à dire avec les Attribut qui vont bien) et clsField à priori une classe quelconque.

    J'imagine que cela demanderait d'accèder "au parent", c'est-à-dire "field.parent -> fields".

    Merci par avance !

  2. #2
    Inactif  

    Homme Profil pro
    cuisiniste
    Inscrit en
    Avril 2009
    Messages
    15 379
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Var (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : cuisiniste
    Secteur : Bâtiment

    Informations forums :
    Inscription : Avril 2009
    Messages : 15 379
    Points : 12 075
    Points
    12 075
    Billets dans le blog
    8
    Par défaut re
    bonsoir
    regarde ceci

    module standard
    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
     
     
    Dim fields As New clsFields
    Sub test()
        fields.Field_Add "toto", "titi"
        fields.Field_Add "riri", "loulou"
        fields.Field_Add "truc", "muche"
        fields.Field_Add "machin ", "chose"
        fields.Field_Add "bidule", "chouette"
       '
       '
       ' le premier et le suivant
        Set f = fields.getfirst
        Set f2 = fields.getnext(f)
        Set f3 = fields.getLast
        Set f2bis = fields.GetByIndex(2)
        Set fx = fields.GetPrevious(f3)
        Set byname = fields.GetByName("truc")
       '
       '
       '
        mess = "premier : " & f.nom & vbCrLf & "   2d  : " & f2.nom & vbCrLf & " dernier : " & f3.nom & vbCrLf & "avec getbyindex(2) : " & f2bis.nom & vbCrLf
        mess = mess & "le GetPrevious de getLast (f3) est :" & fx.nom & vbCrLf
        mess = mess & "avec getbyname(""truc"") :" & byname.prenom
        MsgBox mess
    End Sub
    un module classe comme ca vite fait
    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
     
    Public index As Long
    Public nom As String
    Public prenom As String
    Public getfirst As New clsFields
    Public getLast As New clsFields
    Dim cls() As New clsFields
    Public Function Field_Add(nom, prenom)
        index = index + 1
        ReDim Preserve cls(1 To index)
        cls(index).nom = nom
        cls(index).prenom = prenom
        cls(index).index = index
        If index = 1 Then Set getfirst = cls(index)
        Set getLast = cls(index)
    End Function
    Function getnext(c)
        If c.index = UBound(cls) Then x = 1 Else x = c.index + 1
        Set getnext = cls(x)
    End Function
    Function GetByIndex(x)
        If x > UBound(cls) Then x = 1
        Set GetByIndex = cls(x)
    End Function
     
    Function GetPrevious(c)
        If c.index = 1 Then x = 2: MsgBox "c'est le premier " Else x = c.index
        Set GetPrevious = cls(x - 1)
    End Function
    tu en veux encore??
    mes fichiers dans les contributions:
    mail avec CDO en vba et mail avec CDO en vbs dans un HTA
    survol des bouton dans userform
    prendre un cliché d'un range

    si ton problème est résolu n'oublie pas de pointer : : ça peut servir aux autres
    et n'oublie pas de voter

  3. #3
    Inactif  

    Homme Profil pro
    cuisiniste
    Inscrit en
    Avril 2009
    Messages
    15 379
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Var (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : cuisiniste
    Secteur : Bâtiment

    Informations forums :
    Inscription : Avril 2009
    Messages : 15 379
    Points : 12 075
    Points
    12 075
    Billets dans le blog
    8
    Par défaut re
    j'en rajoute une couche

    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
    Dim fields As New clsFields
    Sub test()
        fields.Field_Add "toto", "titi"
        fields.Field_Add "riri", "loulou"
        fields.Field_Add "truc", "muche"
        fields.Field_Add "machin ", "chose"
        fields.Field_Add "bidule", "chouette"
        ' le premier et le suivant
        Set f = fields.getfirst
        Set f2 = fields.getnext(f)
        Set f3 = fields.getLast
        Set f2bis = fields.GetByIndex(2)
        Set fx = fields.GetPrevious(f3)
        Set byname = fields.GetByName("truc")
        mess = "premier : " & f.nom & vbCrLf & "   2d  : " & f2.nom & vbCrLf & " dernier : " & f3.nom & vbCrLf & "avec getbyindex(2) : " & f2bis.nom & vbCrLf
        mess = mess & "le GetPrevious de getLast (f3) est :" & fx.nom & vbCrLf
        mess = mess & "avec getbyname(""truc"") :" & byname.prenom
        MsgBox mess
    End Sub
    module classe
    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
    34
     
    Public index As Long
    Public nom As String
    Public prenom As String
    Public getfirst As New clsFields
    Public getLast As New clsFields
    Dim cls() As New clsFields
    Public Function Field_Add(nom, prenom)
        index = index + 1
        ReDim Preserve cls(1 To index)
        cls(index).nom = nom
        cls(index).prenom = prenom
        cls(index).index = index
        If index = 1 Then Set getfirst = cls(index)
        Set getLast = cls(index)
    End Function
    Function getnext(c)
        If c.index = UBound(cls) Then x = 1 Else x = c.index + 1
        Set getnext = cls(x)
    End Function
    Function GetByIndex(x)
        If x > UBound(cls) Then x = 1
        Set GetByIndex = cls(x)
    End Function
     
    Function GetPrevious(c)
        If c.index = 1 Then x = 2: MsgBox "c'est le premier " Else x = c.index
        Set GetPrevious = cls(x - 1)
    End Function
    Function GetByName(nom)
        For i = 1 To UBound(cls)
            If cls(i).nom = nom Then Set GetByName = cls(i)
        Next
    End Function
    tu saura faire pour le GetByPrenom

    et j'en rajoute une petite couche on fait la meme chose exactement avec une variable type
    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
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
     
    Public Type Fields
        nom As String
        prenom As String
        Index As Variant
    End Type
    Public ind As Long
    Public pFields() As Fields
    Public FirstField As Fields
    Public LastField As Fields
    Public NextField As Fields
    Sub test()
        field_Add "toto", "aaaaa"
        field_Add "truc", "bbbbb"
        field_Add "bidule", "cccccc"
        field_Add "machin", "dddddd"
        field_Add "chose", "eeeee"
        field_Add "titi", "fffffff"
        'exemple d'utilisation avec des variables
        'les variables doivent etre impérativement dimée en pfields
        NextField = GetNext(FirstField)
        PreviousField = GetPrevious(LastField)
        'directement dans un msgbox
        MsgBox FirstField.nom
        MsgBox LastField.nom
        MsgBox GetNext(FirstField).nom
         MsgBox GetPrevious(LastField).nom
        MsgBox GetFieldByIndex(10).nom
    End Sub
     
    Function field_Add(nom, prenom)
        ind = ind + 1: Index = ind: ReDim Preserve pFields(0 To ind)
        pFields(ind).nom = nom
        pFields(ind).prenom = prenom
        pFields(ind).Index = ind
        If ind = 1 Then FirstField = pFields(ind)
        LastField = pFields(ind)
        pFields(0).nom = "existe pas"
        pFields(0).prenom = "existe pas"
        pFields(0).Index = "index hors limite"
     
    End Function
    Function GetNext(f As Fields) As Fields
        If f.Index = UBound(pFields) Then x = 0 Else x = f.Index + 1
        GetNext = pFields(x)
    End Function
    Function GetPrevious(f As Fields) As Fields
        If f.Index <= 1 Then x = 0 Else x = f.Index - 1
        GetPrevious = pFields(x)
    End Function
    Function GetFieldByIndex(x As Long) As Fields
        If x > UBound(pFields) Or x < 1 Then x = 0
        GetFieldByIndex = pFields(x)
    End Function
    mes fichiers dans les contributions:
    mail avec CDO en vba et mail avec CDO en vbs dans un HTA
    survol des bouton dans userform
    prendre un cliché d'un range

    si ton problème est résolu n'oublie pas de pointer : : ça peut servir aux autres
    et n'oublie pas de voter

Discussions similaires

  1. Réponses: 4
    Dernier message: 05/08/2012, 18h08
  2. Réponses: 7
    Dernier message: 25/03/2011, 14h04
  3. [MooTools] AddEvent sur un element dans une Class : this n'est pas l'objet courant
    Par Neilime05 dans le forum Bibliothèques & Frameworks
    Réponses: 6
    Dernier message: 12/01/2011, 12h20
  4. Réponses: 10
    Dernier message: 24/09/2009, 18h49
  5. appel des elements d'une classe à partir d'une autre
    Par oceane751 dans le forum Langage
    Réponses: 4
    Dernier message: 02/01/2008, 21h52

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