By calling the method each time that you use the collection, what your code is effectively doing is:
For i As Integer = 0 To csB.Count - 1
Dim cc As Collection(Of Reports_ucColumn)
If csB(i).Contains("Invalid") Or csB(i).Contains("Duplicate") Then
cc = New Collection(Of Reports_ucColumn)
cc.Add(Me.ucColumn0)
cc.Add(Me.ucColumn1)
cc.Add(Me.ucColumn2)
cc.Item(i).isValid = False
cc = New Collection(Of Reports_ucColumn)
cc.Add(Me.ucColumn0)
cc.Add(Me.ucColumn1)
cc.Add(Me.ucColumn2)
cc.Item(i).title = csB(i)
If csB(i).Contains("Invalid") Then
cc = New Collection(Of Reports_ucColumn)
cc.Add(Me.ucColumn0)
cc.Add(Me.ucColumn1)
cc.Add(Me.ucColumn2)
cc.Item(i).errCode = "005W"
Else
cc = New Collection(Of Reports_ucColumn)
cc.Add(Me.ucColumn0)
cc.Add(Me.ucColumn1)
cc.Add(Me.ucColumn2)
cc.Item(i).errCode = "005W" 'todo - chg this once we get a duplicate col err code
End If
Else
cc = New Collection(Of Reports_ucColumn)
cc.Add(Me.ucColumn0)
cc.Add(Me.ucColumn1)
cc.Add(Me.ucColumn2)
cc.Item(i).isValid = True
cc = New Collection(Of Reports_ucColumn)
cc.Add(Me.ucColumn0)
cc.Add(Me.ucColumn1)
cc.Add(Me.ucColumn2)
cc.Item(i).title = csB(i)
cc = New Collection(Of Reports_ucColumn)
cc.Add(Me.ucColumn0)
cc.Add(Me.ucColumn1)
cc.Add(Me.ucColumn2)
cc.Item(i).errCode = String.Empty
End If
Next
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:
Private _cc As Collection(Of Reports_ucColumn)
Private Property CcB As Collection(Of Reports_ucColumn)
Get
If _cc Is Nothing Then
_cc = New Collection(Of Reports_ucColumn)
_cc.Add(Me.ucColumn0)
_cc.Add(Me.ucColumn1)
_cc.Add(Me.ucColumn2)
End If
Return _cc
End Get
End Property
(Ideally the property should be public in a class and the _cc
variable 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.