I’ve got a f(x) that returns a collection of user controls. .Net lets me just treat the f(x) name as the collection. Is there something wrong with doing so?
ex)
Private Function GetCcB() As Collection(Of Reports_ucColumn)
Dim cc As New Collection(Of Reports_ucColumn)
cc.Add(Me.ucColumn0)
cc.Add(Me.ucColumn1)
cc.Add(Me.ucColumn2)
Return cc
End Function
Then later on, all use the properties on the objects in the collection, like this:
For i As Integer = 0 To csB.Count - 1
If csB(i).Contains("Invalid") Or csB(i).Contains("Duplicate") Then
GetCcB.Item(i).isValid = False
GetCcB.Item(i).title = csB(i)
If csB(i).Contains("Invalid") Then : GetCcB.Item(i).errCode = "005W"
Else : GetCcB.Item(i).errCode = "005W" 'todo - chg this once we get a duplicate col err code
End If
Else
GetCcB.Item(i).isValid = True
GetCcB.Item(i).title = csB(i)
GetCcB.Item(i).errCode = String.Empty
End If
By calling the method each time that you use the collection, what your code is effectively doing is:
You would never write code like that, would you?
You can turn the function into a property with lazy initialisation. Then it’s ok to use it over and over, as the collection is only created once:
(Ideally the property should be public in a class and the
_ccvariable should be private, encapsulated in the class so that only the property could access it.)Semantically this works fine also. A function generally does a bit of work, so you shouldn’t call it over and over if you want to use the result over and over. A property on the other hand usually doesn’t do much work, and if the result needs some processing to be created, the property usually does something like lazy initialisation or pre-initialisation to make sure that the work isn’t done more than neccessary.