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 :

[Débutant] Fonction qui retourne un objet (classe)


Sujet :

Macros et VBA Excel

  1. #1
    in
    in est déconnecté
    Membre expérimenté Avatar de in
    Profil pro
    Inscrit en
    Avril 2003
    Messages
    1 612
    Détails du profil
    Informations personnelles :
    Localisation : France, Finistère (Bretagne)

    Informations forums :
    Inscription : Avril 2003
    Messages : 1 612
    Points : 1 718
    Points
    1 718
    Par défaut [Débutant] Fonction qui retourne un objet (classe)
    Bonjour,

    c'est sûrement une question bête mais je coince vraiment ...

    J'ai un module de classe "Project".
    J'ai un module où j'ai défini une fonction qui doit me retourner ( ~créer) un objet Project.

    Voici la fonction :

    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
    ' renvoie un objet projet créé à partir des données contenues à la ligne line
    ' de la feuille sheet (interface_p3e normalement)
    Public Function getProject(sheet As Worksheet, line As Integer) As Project
     
        Dim projet As New Project
     
        ' initialisation des membres d'un objet project
     
        With sheet
            projet.Prog = .Cells(line, 16)
            projet.Proj = .Cells(line, 2)
            projet.Init_id = .Cells(line, 14)
            projet.Last_id = .Cells(line, 15)
            projet.OBS = .Cells(line, 12)
        End With
        ' jusqu'à la en débug, tous les champs de l'objet sont correctement initialisés (enfin il y a des valeurs quoi)
     
        ' renvoie le project  --> c'est la que ça coince
        getProject = projet
     
    End Function
    Quand je l'exécute, au moment de retourner la valeur il me dit :

    "Variable objet ou bloc with non définie" (sur le getProject quand je passe la souris dessus)

    C'est pas possible ce que je veux faire ?

    Merci d'avance pour vos suggestions.
    "If email had been around before the telephone was invented, people would have said, 'Hey, forget email! With this new telephone invention I can actually talk to people!"

    Besoin d'une nouvelle méthode pour développer ? -> http://www.la-rache.com/

  2. #2
    Membre éclairé

    Profil pro
    Inscrit en
    Mai 2007
    Messages
    514
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2007
    Messages : 514
    Points : 824
    Points
    824
    Par défaut
    Bonjour,

    Si la fonction doit retourner un objet alors il faut utiliser le mot-clé Set.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    Public Function getProject(sheet As Worksheet, line As Integer) As Project
     
        Dim projet As New Project
     
        Set getProject = projet
     
    End Function
    Cordialment,

    Tirex28/

  3. #3
    in
    in est déconnecté
    Membre expérimenté Avatar de in
    Profil pro
    Inscrit en
    Avril 2003
    Messages
    1 612
    Détails du profil
    Informations personnelles :
    Localisation : France, Finistère (Bretagne)

    Informations forums :
    Inscription : Avril 2003
    Messages : 1 612
    Points : 1 718
    Points
    1 718
    Par défaut
    J'avais essayé le Set mais pas au bon endroit ...

    Merci en tous cas, je me disais bien que c'était faisable ...
    "If email had been around before the telephone was invented, people would have said, 'Hey, forget email! With this new telephone invention I can actually talk to people!"

    Besoin d'une nouvelle méthode pour développer ? -> http://www.la-rache.com/

  4. #4
    in
    in est déconnecté
    Membre expérimenté Avatar de in
    Profil pro
    Inscrit en
    Avril 2003
    Messages
    1 612
    Détails du profil
    Informations personnelles :
    Localisation : France, Finistère (Bretagne)

    Informations forums :
    Inscription : Avril 2003
    Messages : 1 612
    Points : 1 718
    Points
    1 718
    Par défaut
    Euh, j'ai un petit souci du même ordre, donc je reprends ce sujet ...

    j'ai une autre classe Scope avec :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    ' la liste des projets
    Private liste_projets As New Collection
     
    Public Sub addProject(ByRef p As Project)
        liste_projets.Add (p)
    End Sub
    Dans un module je souhaite ajouter un Projet à au Scope en faisant :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    scope.addProject (getProject(ActiveSheet, index))
    (scope est préalablement créé)

    J'obtiens l'erreur : propriété ou méthode non gérée par cet objet

    Désolé je suis peut être trop habituer à la prog objet mais là avec VBA je m'arrache les cheveux ...
    "If email had been around before the telephone was invented, people would have said, 'Hey, forget email! With this new telephone invention I can actually talk to people!"

    Besoin d'une nouvelle méthode pour développer ? -> http://www.la-rache.com/

  5. #5
    Membre éclairé

    Profil pro
    Inscrit en
    Mai 2007
    Messages
    514
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2007
    Messages : 514
    Points : 824
    Points
    824
    Par défaut
    Re,

    Tu peux nous joindre un exemple de ton fichier ?

    Cordialement,

    Tirex28/

  6. #6
    in
    in est déconnecté
    Membre expérimenté Avatar de in
    Profil pro
    Inscrit en
    Avril 2003
    Messages
    1 612
    Détails du profil
    Informations personnelles :
    Localisation : France, Finistère (Bretagne)

    Informations forums :
    Inscription : Avril 2003
    Messages : 1 612
    Points : 1 718
    Points
    1 718
    Par défaut
    Ben pas vraiment.

    Le code important de Scope vous l'avez.
    Le code de la méthode getProject vous l'avez.

    Voici un morceau de la classe Project :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    Private lProg As String 
    Private lProj As String 
    Private lOBS  As String                 
    Private lInit_id As Long              
    Private lLast_id As Long         
    Private lIsProjectMR As Boolean          
    Private lInit_typed_WBS As New Collection  'non initialisé lors du getProject
    Private lLast_typed_WBS As New Collection 'non initialisé lors du getProject 
     
    ' suivent les getter/setter associés

    Et là le morceau du module qui utilise tous ces trucs :

    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
    Global scope As New scope   'Scope du reporting (liste des projets)
     
    Public Sub P3E_Init_Dashboard_update()
     
        Dim index As Integer
     
       '...
     
        index = index + 1
     
        ' on récupère la liste des projets (baselines) définis dans le scope, si la liste est vide
        ' pour construire la collection de projet (nécessaire si on a pas utilisé le check baseline)
        If scope.nbProjects = 0 Then
            With Worksheets(Feuille_Interface)
                Do Until .Cells(index, 1).Interior.ColorIndex = Colors.rouge Or .Cells(index, 2) = ""
                    scope.addProject (getProject(ActiveSheet, index))  'ERREUR !!
                    index = index + 1
                Loop
            End With
        End If
    '...
    et voilà, c'est tout ce qu'il faut je pense ...
    "If email had been around before the telephone was invented, people would have said, 'Hey, forget email! With this new telephone invention I can actually talk to people!"

    Besoin d'une nouvelle méthode pour développer ? -> http://www.la-rache.com/

  7. #7
    Membre éclairé

    Profil pro
    Inscrit en
    Mai 2007
    Messages
    514
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2007
    Messages : 514
    Points : 824
    Points
    824
    Par défaut
    Re,

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    Global scope As New scope
    Global je connaissais pas...

    Par contre il faudrait peut etre eviter de nommer ta variable comme ta classe.

    Sinon essaye ceci, j'ai simplifie mais tu devrais pouvoir adapter.

    Dans le module scope:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    Private liste_projets As Collection
     
    Property Get ListeProjet() As Collection
        Set ListeProjet = liste_projets
    End Property
     
    Private Sub Class_Initialize()
        Set liste_projets = New Collection
    End Sub
    Dans le module project:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    Private Const K_Exemple As String = "exemple"
     
    Property Get Exemple() As String
        Exemple = K_Exemple
    End Property
    Dans un module standard:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
    Sub AjouterProjet()
        Dim myscope As New scope
        myscope.ListeProjet.Add getProject
        MsgBox myscope.ListeProjet(1).Exemple
    End Sub
     
    Public Function getProject() As project
        Dim projet As New project
        Set getProject = projet
    End Function
    AjouterProjet ajoute un element de type project a la collection listeprojet de myscope.

    Bonne chance,
    Tirex28/

  8. #8
    in
    in est déconnecté
    Membre expérimenté Avatar de in
    Profil pro
    Inscrit en
    Avril 2003
    Messages
    1 612
    Détails du profil
    Informations personnelles :
    Localisation : France, Finistère (Bretagne)

    Informations forums :
    Inscription : Avril 2003
    Messages : 1 612
    Points : 1 718
    Points
    1 718
    Par défaut
    Effectivement en donnant un accesseur à la liste des projets du scope, je peux ajouter un projet.

    Donc ça marche. Merci beaucoup.

    Par contre pourquoi ma méthode addProject ne fonctionne pas ???
    "If email had been around before the telephone was invented, people would have said, 'Hey, forget email! With this new telephone invention I can actually talk to people!"

    Besoin d'une nouvelle méthode pour développer ? -> http://www.la-rache.com/

  9. #9
    Membre éclairé

    Profil pro
    Inscrit en
    Mai 2007
    Messages
    514
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2007
    Messages : 514
    Points : 824
    Points
    824
    Par défaut
    Bonsoir,

    Apres avoir mieux regardé il ne s'agissait que d'un probleme de syntaxe, des parentheses en trop.

    Dans un module standard:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
    Sub AjouterProjet()
        Dim myscope As New scope
        myscope.addProject getProject 'getProject n'est pas entre parentheses
        MsgBox myscope.liste_projets.Item(1).Exemple
    End Sub
     
    Public Function getProject() As project
        Dim projet As New project
        Set getProject = projet
    End Function
    Dans le module scope:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    Public liste_projets As New Collection
     
    Public Sub addProject(ByRef p As project)
        liste_projets.Add p 'Le p n'est pas entre parentheses
    End Sub
    Dans le module project:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    Private Const K_Exemple As String = "exemple"
     
    Property Get Exemple() As String
        Exemple = K_Exemple
    End Property
    Cordialement,

    Tirex28/

  10. #10
    in
    in est déconnecté
    Membre expérimenté Avatar de in
    Profil pro
    Inscrit en
    Avril 2003
    Messages
    1 612
    Détails du profil
    Informations personnelles :
    Localisation : France, Finistère (Bretagne)

    Informations forums :
    Inscription : Avril 2003
    Messages : 1 612
    Points : 1 718
    Points
    1 718
    Par défaut
    Effectivement c'était bien ça ...

    Mais alors je suis un peu perdu ... des fois j'utilise des fonctions avec des paranthèses et ça marche très bien. D'ailleurs quand je mets la paranthèse, l'éditeur de code m'affiche la liste des paramètres ... enfin je veux dire, ça me parait normal de mettre les paranthèses ...

    Bref, est ce qu'il y a une règle ?

    En tous cas merci bien. Je débute en vba et je ne crois pas qu'on sera super amis lui et moi ...

    A+ et merci encore
    "If email had been around before the telephone was invented, people would have said, 'Hey, forget email! With this new telephone invention I can actually talk to people!"

    Besoin d'une nouvelle méthode pour développer ? -> http://www.la-rache.com/

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Fonction qui retourne plusieurs valeurs !
    Par casafa dans le forum C++
    Réponses: 20
    Dernier message: 23/04/2014, 16h56
  2. Fonction qui retourne un objet complexe
    Par Mister Nono dans le forum Services Web
    Réponses: 5
    Dernier message: 23/04/2009, 14h33
  3. Réponses: 2
    Dernier message: 16/03/2009, 14h37
  4. Réponses: 7
    Dernier message: 03/12/2004, 12h40
  5. Réponses: 14
    Dernier message: 09/04/2004, 13h44

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