首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何优化代码

如何优化代码
EN

Stack Overflow用户
提问于 2014-05-03 19:58:40
回答 1查看 58关注 0票数 0

我有以下代码行:

代码语言:javascript
复制
 Protected Sub RepComisiones_ItemDataBound(sender As Object, e As RepeaterItemEventArgs) Handles RepComisiones.ItemDataBound
    Dim valoresRepeter As DataRowView


    If e.Item.ItemType = ListItemType.Item Or (e.Item.ItemType = ListItemType.AlternatingItem) Then
        valoresRepeter = e.Item.DataItem

        Select Case valoresRepeter("ECO").ToString
            Case "0"
                CType(e.Item.FindControl("lblEco"), Label).Text = ""
        End Select
        Select Case valoresRepeter("A").ToString
            Case 0
                CType(e.Item.FindControl("lblA"), Label).Text = ""
        End Select
        Select Case valoresRepeter("B1").ToString
            Case 0
                CType(e.Item.FindControl("lblB1"), Label).Text = ""
        End Select
        Select Case valoresRepeter("B2").ToString
            Case 0
                CType(e.Item.FindControl("lblB2"), Label).Text = ""
        End Select
        Select Case valoresRepeter("B3").ToString
            Case 0
                CType(e.Item.FindControl("lblB3"), Label).Text = ""
        End Select
        Select Case valoresRepeter("B3P").ToString
            Case 0
                CType(e.Item.FindControl("lblB3P"), Label).Text = ""
        End Select

    End If
End Sub

我想减少一些线,我尝试不同的方式,但结果是不正确的,任何想法,我如何优化。

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-04 11:25:31

一个简单而干净的解决方案是将所有键("ECO“、"A”、"B1")及其相应的控件名称("lblECO“、"lblA”、"lblB")放在字典中,然后遍历这些键以求出各个条件。

代码语言:javascript
复制
Private mappings As New Dictionary(Of String, String) From
        {
            {"ECO", "lblEco"},
            {"A", "lblA"},
            {"B1", "lblB1"}
        }

Sub RepComisiones_ItemDataBound()

        For Each key As String In mappings.Keys
            If valoresRepeter(key).ToString Is "0" Then
                DirectCast(e.Item.FindControl(mappings.Item(key)), Label).Text = ""
            End If
        Next

    End Sub
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23449164

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档