In my example, Method1 uses a variable to store the return values and then assigns the variable to the function. Method2 adds the return values directly to the function.
Questions:
- What is the pattern of Method2 called?
- Is one method prefered over the other and why?
Function Method1(FirstNumber As Long, LastNumber As Long) As Collection
Dim Map As New Collection
Dim n As Long
For n = FirstNumber To LastNumber
Method1.Add n
Next
Set Method1 = Map
End Function
Function Method2(FirstNumber As Long, LastNumber As Long) As Collection
Set Method2 = Collection
Dim n As Long
For n = FirstNumber To LastNumber
Method2.Add n
Next
End Function
Addendum
Could someone explain the votes to close the question? I have written well over 50 of these types of functions this year alone. Not to mention that as a contributor to SO, I feel like hearing from other developers opinions on the subject will help me improve my answers.
Feel free to down vote and vote to close the question again for me commenting in my post.
Edit 1
There was a typo in my original code. This is how Method1
should have been written.
Function Method1(FirstNumber As Long, LastNumber As Long) As Collection
Dim Map As New Collection
Dim n As Long
For n = FirstNumber To LastNumber
Map.Add n
Next
Set Method1 = Map
End Function
I restored my original post and added this edit for clarification.