I am converting an old Vb6 solution to .net 2.0 in vs2010. I've been working in C# for about 3 years now and .net for 5. I don't recall having this problem in C#
, but if I want initialize a readonly
collection of DerivedControlFoo
Is there a clean way to do it besides creating a sub to do it all off somewhere else? I'd love to be able to do it at the class level at the declaration for readability and simplicity.
Example:
Private _formattedFormulaText As IEnumerable(Of Label) = New List(Of Label) From { _
FormulaLabels0, FormulaLabels1, lblBrownFormula, FormulaLabels3, lblGreenFormula, _
lblOrangeFormula, lblRedFormula, FormulaLabels7, lblFormulaTotal}
Doing it the straightforward easy way, results in the collection being filled with {nothing,..,nothing}
This is not thread safe, but works very well for my purposes:
Solution in use:
drawbacks - all code touching the variable has to add an accessor
.value
. My code has 5 of these lazy collections and 1-2 touchpoints per collection usually in the same function.Drawback illustration:
becomes
Not compliant with good practices for debugger display used in .net 4, but would be easy to add in using reflector
Please note a property could have been used to eliminate the need to .value in the touchpoints:
supporting class:
design based on MSDN's article Lazy Computation in C# Also with assistance from Reflector against