FONCTION QUI RETOURNE UN TABLEAU

Résolu
bouv Messages postés 1411 Date d'inscription mercredi 6 août 2003 Statut Membre Dernière intervention 3 mars 2019 - 15 oct. 2007 à 14:58
Renfield Messages postés 17287 Date d'inscription mercredi 2 janvier 2002 Statut Modérateur Dernière intervention 27 septembre 2021 - 15 oct. 2007 à 16:16
Salut,

Je souhaite créer une fonction qui retourne un tableau.

Pour l'instant j'utilise une procédure avec un tableau passé en ByRef pour le retour :
Public Sub MaFonction (ByVal Index as Integer, ByRef RetArray( ) as MonTypePerso)

Existe-t-il un moyen de passer par une 'vraie fonction' ? Quelle syntaxe dois-je utiliser ?

Merci d'avance
Bonne prog
++

3 réponses

Renfield Messages postés 17287 Date d'inscription mercredi 2 janvier 2002 Statut Modérateur Dernière intervention 27 septembre 2021 74
15 oct. 2007 à 16:16
Tableau de classe.... oui, si tu veux,

Private Sub Form_Load()
Dim i As Long
For i = 0 To UBound(MaFonction)
Debug.Print MaFonction(i).Value
Next i

End Sub

Public Function MaFonction() As Variant
Dim xnRet(5) As Class1
Set xnRet(0) New Class1: xnRet(0).Value 12
Set xnRet(1) New Class1: xnRet(1).Value 5
Set xnRet(2) New Class1: xnRet(2).Value 7
Set xnRet(3) New Class1: xnRet(3).Value 6
Set xnRet(4) New Class1: xnRet(4).Value 2
Set xnRet(5) New Class1: xnRet(5).Value 9

MaFonction = xnRet
End Function

mais on preferera utiliser une collection :

Private Sub Form_Load()
Dim oVar1 As Class1
For Each oVar1 In MaFonction
Debug.Print oVar1.Value
Next oVar1
End Sub

Public Function MaFonction() As Collection
Dim oVar1 As Class1
Set MaFonction = New Collection
Set oVar1 = New Class1
oVar1.Value = 12
MaFonction.Add oVar1

Set oVar1 = New Class1
oVar1.Value = 5
MaFonction.Add oVar1

Set oVar1 = New Class1
oVar1.Value = 7
MaFonction.Add oVar1

Set oVar1 = New Class1
oVar1.Value = 6
MaFonction.Add oVar1

Set oVar1 = New Class1
oVar1.Value = 2
MaFonction.Add oVar1

Set oVar1 = New Class1
oVar1.Value = 9
MaFonction.Add oVar1
End Function
3
Renfield Messages postés 17287 Date d'inscription mercredi 2 janvier 2002 Statut Modérateur Dernière intervention 27 septembre 2021 74
15 oct. 2007 à 15:45
'lut Bouv

ce genre de chose est faisable, passer par un Variant :

Public Function MaFonction() As Variant
Dim xnRet(5) As Long
xnRet(0) = 12
xnRet(1) = 5
xnRet(2) = 7
xnRet(3) = 6
xnRet(4) = 2
xnRet(5) = 9

MaFonction = xnRet
End Function
0
bouv Messages postés 1411 Date d'inscription mercredi 6 août 2003 Statut Membre Dernière intervention 3 mars 2019 1
15 oct. 2007 à 16:02
Salut et merci,
Ceci est-il également valable avec un type fait maison (autre que Long, String...) ou pour renvoyer un tableau d'objet (class) par exemple ?

Bonne prog
++
0
Rejoignez-nous