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
++
A voir également:
Fonction qui retourne un tableau en c
Fonction qui retourne un tableau - Meilleures réponses
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
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 ?