在初始化组件之前初始化表单类级别组件集合的干净优雅的解决方案?

发布于 2024-10-15 11:01:09 字数 590 浏览 1 评论 0原文

我正在 vs2010 中将旧的 Vb6 解决方案转换为 .net 2.0。我已经在 C# 领域工作了大约 3 年,在 .net 领域工作了 5 年。我不记得在 C# 中遇到过这个问题,但如果我想初始化一个 readonly DerivedControlFoo 的集合 除了创建一个子程序在其他地方完成这一切之外,是否有一种干净的方法可以做到这一点?为了可读性和简单性,我希望能够在声明的类级别上执行此操作。

示例:

Private _formattedFormulaText As IEnumerable(Of Label) = New List(Of Label) From { _
 FormulaLabels0, FormulaLabels1, lblBrownFormula, FormulaLabels3, lblGreenFormula, _
      lblOrangeFormula, lblRedFormula, FormulaLabels7, lblFormulaTotal}

以简单的方式进行操作,会导致集合填充 {nothing,..,nothing}

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}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

著墨染雨君画夕 2024-10-22 11:01:09

这不是线程安全的,但非常适合我的目的:

使用中的解决方案:

 Private _formattedFormulaText As New Lazy(Of IEnumerable(Of Label))(Function() New List(Of Label) From { _
 FormulaLabels0, FormulaLabels1, lblBrownFormula, FormulaLabels3, lblGreenFormula, _
      lblOrangeFormula, lblSRedFormula, FormulaLabels7, lblFormulaTotal})

缺点 - 所有涉及变量的代码都必须添加访问器.value。我的代码有 5 个惰性集合,每个集合有 1-2 个接触点,通常位于同一函数中。

缺点说明:

Dim clearText = Sub(c As Control) c.Text = String.Empty
        _formattedFormulaText.ToList.ForEach(clearText)

变得

Dim clearText = Sub(c As Control) c.Text = String.Empty
        _formattedFormulaText.Value.ToList.ForEach(clearText)

不符合 .net 4 中使用的调试器显示的良好实践,但使用反射器可以轻松添加


请注意,可以使用属性来消除接触点中 .value 的需要:

   Private ReadOnly Property FormattedText As IEnumerable(Of Label)
        Get
            Return _formattedFormulaText.Value
        End Get
    End Property

支持类:

''' <summary>
''' translated from http://msdn.microsoft.com/en-us/vcsharp/bb870976.aspx
''' </summary>
Public Class Lazy(Of T)
    Private _func As Func(Of T)
    Private _result As T
    Private _hasValue As Boolean
    Public Sub New(ByVal func As Func(Of T))
        _func = func
        _hasValue = False
    End Sub
    Public ReadOnly Property Value As T
        Get
            If Me._hasValue = False Then
                _result = _func()
                _hasValue = True
            End If
            Return _result
        End Get
    End Property

End Class

设计基于 MSDN 的文章 C# 中的延迟计算
另外在 Reflector 的帮助下

assembly\NativeImages_v4.0.30319_32\mscorlib\246f1a5abb686b9dcdf22d3505b08cea\mscorlib.ni.dll

This is not thread safe, but works very well for my purposes:

Solution in use:

 Private _formattedFormulaText As New Lazy(Of IEnumerable(Of Label))(Function() New List(Of Label) From { _
 FormulaLabels0, FormulaLabels1, lblBrownFormula, FormulaLabels3, lblGreenFormula, _
      lblOrangeFormula, lblSRedFormula, FormulaLabels7, lblFormulaTotal})

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:

Dim clearText = Sub(c As Control) c.Text = String.Empty
        _formattedFormulaText.ToList.ForEach(clearText)

becomes

Dim clearText = Sub(c As Control) c.Text = String.Empty
        _formattedFormulaText.Value.ToList.ForEach(clearText)

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:

   Private ReadOnly Property FormattedText As IEnumerable(Of Label)
        Get
            Return _formattedFormulaText.Value
        End Get
    End Property

supporting class:

''' <summary>
''' translated from http://msdn.microsoft.com/en-us/vcsharp/bb870976.aspx
''' </summary>
Public Class Lazy(Of T)
    Private _func As Func(Of T)
    Private _result As T
    Private _hasValue As Boolean
    Public Sub New(ByVal func As Func(Of T))
        _func = func
        _hasValue = False
    End Sub
    Public ReadOnly Property Value As T
        Get
            If Me._hasValue = False Then
                _result = _func()
                _hasValue = True
            End If
            Return _result
        End Get
    End Property

End Class

design based on MSDN's article Lazy Computation in C#
Also with assistance from Reflector against

assembly\NativeImages_v4.0.30319_32\mscorlib\246f1a5abb686b9dcdf22d3505b08cea\mscorlib.ni.dll
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文