Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>By calling the method each time that you use the collection, what your code is effectively doing is:</p> <pre><code>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 </code></pre> <p>You would never write code like that, would you?</p> <p>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:</p> <pre><code>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 </code></pre> <p>(Ideally the property should be public in a class and the <code>_cc</code> variable should be private, encapsulated in the class so that only the property could access it.)</p> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload