2

我正在使用 ASP Web 表单和 VB 后端创建太阳能光伏计算器。我只是查看了我的代码,作为一个新手 VB.NET 开发人员,我只是想知道是否有办法做一些不同的事情,减少代码行数或类似的事情。这是 y 代码的一个繁琐部分:

Dim SR As Integer
    'Store radiation value
    If drpDwnRoofFacing.Text = "South" And drpAngleOfRoof.Text = "Horizontal" Then
        SR = 933
    ElseIf drpDwnRoofFacing.Text = "SE/SW" And drpAngleOfRoof.Text = "Horizontal" Then
        SR = 933
    ElseIf drpDwnRoofFacing.Text = "E/W" And drpAngleOfRoof.Text = "Horizontal" Then
        SR = 933
    ElseIf drpDwnRoofFacing.Text = "NE/NW" And drpAngleOfRoof.Text = "Horizontal" Then
        SR = 933
    ElseIf drpDwnRoofFacing.Text = "North" And drpAngleOfRoof.Text = "Horizontal" Then
        SR = 933
    ElseIf drpDwnRoofFacing.Text = "South" And drpAngleOfRoof.Text = "30 Degrees" Then
        SR = 1042
    ElseIf drpDwnRoofFacing.Text = "SE/SW" And drpAngleOfRoof.Text = "30 Degrees" Then
        SR = 997
    ElseIf drpDwnRoofFacing.Text = "E/W" And drpAngleOfRoof.Text = "30 Degrees" Then
        SR = 886
    ElseIf drpDwnRoofFacing.Text = "NE/NW" And drpAngleOfRoof.Text = "30 Degrees" Then
        SR = 762
    ElseIf drpDwnRoofFacing.Text = "North" And drpAngleOfRoof.Text = "30 Degrees" Then
        SR = 709
    ElseIf drpDwnRoofFacing.Text = "South" And drpAngleOfRoof.Text = "45 Degrees" Then
        SR = 1023
    ElseIf drpDwnRoofFacing.Text = "SE/SW" And drpAngleOfRoof.Text = "45 Degrees" Then
        SR = 968
    ElseIf drpDwnRoofFacing.Text = "E/W" And drpAngleOfRoof.Text = "45 Degrees" Then
        SR = 829
    ElseIf drpDwnRoofFacing.Text = "NE/NW" And drpAngleOfRoof.Text = "45 Degrees" Then
        SR = 666
    ElseIf drpDwnRoofFacing.Text = "North" And drpAngleOfRoof.Text = "45 Degrees" Then
        SR = 621
    ElseIf drpDwnRoofFacing.Text = "South" And drpAngleOfRoof.Text = "60 Degrees" Then
        SR = 960
    ElseIf drpDwnRoofFacing.Text = "SE/SW" And drpAngleOfRoof.Text = "60 Degrees" Then
        SR = 900
    ElseIf drpDwnRoofFacing.Text = "E/W" And drpAngleOfRoof.Text = "60 Degrees" Then
        SR = 753
    ElseIf drpDwnRoofFacing.Text = "NE/NW" And drpAngleOfRoof.Text = "60 Degrees" Then
        SR = 580
    ElseIf drpDwnRoofFacing.Text = "North" And drpAngleOfRoof.Text = "60 Degrees" Then
        SR = 485
    ElseIf drpDwnRoofFacing.Text = "South" And drpAngleOfRoof.Text = "Vertical" Then
        SR = 724
    ElseIf drpDwnRoofFacing.Text = "SE/SW" And drpAngleOfRoof.Text = "Vertical" Then
        SR = 684
    ElseIf drpDwnRoofFacing.Text = "E/W" And drpAngleOfRoof.Text = "Vertical" Then
        SR = 565
    ElseIf drpDwnRoofFacing.Text = "NE/NW" And drpAngleOfRoof.Text = "Vertical" Then
        SR = 427
    ElseIf drpDwnRoofFacing.Text = "North" And drpAngleOfRoof.Text = "Vertical" Then
        SR = 360
    End If
4

4 回答 4

5

我建议将此数据存储在应用程序代码之外,例如存储在数据库中,或者如果没有必要,存储在 XML 文件中。但是,即使您要在代码中硬编码转换规则,最好将规则的硬编码与处理和应用规则的逻辑分开。例如,您可以创建一个包含规则的类,如下所示:

Public Class MyRule
    Public Sub New(roofFacing As String, angleOfRoof As String, SR As Integer)
        _roofFacing = roofFacing
        _angleOfRoof = angleOfRoof
        _SR = sr
    End New

    Public ReadOnly Property RoofFacing() As String
        Get
            Return _roofFacing
        End Get
    End Property
    Private _roofFacing As String

    Public ReadOnly Property AngleOfRoof() As String
        Get
            Return _angleOfRoof
        End Get
    End Property
    Private _angleOfRoofAs String

    Public ReadOnly Property SR() As String
        Get
            Return _SR
        End Get
    End Property
    Private _SR String
End Class

然后,您可以对规则列表的创建进行硬编码,就好像它们来自数据库一样,如下所示:

Dim rules As New List(Of MyRule)()
rules.Add(New MyRule("South", "Horizontal", 933))
rules.Add(New MyRule("South", "SE/SW", 933))
' ...
rules.Add(New MyRule("South", Nothing, 933))

If然后,您可以使用一个简单的循环来应用规则,而不是一个巨大的语句,如下所示:

Dim SR As Integer
For Each rule As MyRule In rules
    If _
    ( _
        (rule.RoofFacing Is Nothing) OrEsle _
        (rule.RoofFacing = drpDwnRoofFacing.Text) _
    ) AndAlso _
    ( _
        (rule.AngleOfRoof Is Nothing) OrEsle _
        (rule.AngleOfRoof = drpAngleOfRoof.Text) _
    ) Then
        SR = rule.SR
        Exit For
    End If
Next

如您所见,一旦以这种方式进行设计,以后修改规则或添加其他规则会容易得多。但是,您应该真正为这些值使用枚举,而不是字符串。例如:

Public Enum RoofFacings
    Any
    North
    South
    EastOrWest
    ' ...
End Enum

Public Enum AnglesOfRoof
    Any
    Horizontal
    Degrees30
    Degrees45
    ' ...
    Vertical
End Enum
于 2013-01-28T14:09:56.060 回答
3
Select Case drpAngleOfRoof.Text
        Case "Horizontal"
            Select Case drpDwnRoofFacing.Text
                Case "South" : SR = 933
                Case "SE/SW" : SR = 933
                Case "E/W" : SR = 933
                Case "NE/NW" : SR = 933
                Case "North" : SR = 933
            End Select
        Case "30 Degrees"
            Select Case drpDwnRoofFacing.Text
                Case "South" : SR = 933
                Case "SE/SW" : SR = 933
                Case "E/W" : SR = 933
                Case "NE/NW" : SR = 933
                Case "North" : SR = 933
            End Select
        Case "45 Degrees"
            Select Case drpDwnRoofFacing.Text
                Case "South" : SR = 933
                Case "SE/SW" : SR = 933
                Case "E/W" : SR = 933
                Case "NE/NW" : SR = 933
                Case "North" : SR = 933
            End Select
        Case "60 Degrees"
            Select Case drpDwnRoofFacing.Text
                Case "South" : SR = 933
                Case "SE/SW" : SR = 933
                Case "E/W" : SR = 933
                Case "NE/NW" : SR = 933
                Case "North" : SR = 933
            End Select
        Case "Vertical"
            Select Case drpDwnRoofFacing.Text
                Case "South" : SR = 933
                Case "SE/SW" : SR = 933
                Case "E/W" : SR = 933
                Case "NE/NW" : SR = 933
                Case "North" : SR = 933
            End Select
    End Select
于 2013-01-28T14:06:28.217 回答
2

您可以嵌套 IF 语句,即

If drpAngleOfRoof.Text = "Horizontal" Then

    If drpDwnRoofFacing.Text = "South" Then

         SR = 933

    End If

End If

例如,将与每个角度位置相关的所有语句组合在一起。

或者,您可以使用 Select...Case 语句作为替代。

http://msdn.microsoft.com/en-us/library/cy37t14y(v=vs.71).aspx

于 2013-01-28T13:51:20.053 回答
-1

不如使用 select 语句来代替。您将最初的选择削减为drpAngleOfRoof 的值,然后您只需要担心drpDownRoofFacing 的值。像这样的东西:

Select Case drpAngleOfRoof.Text
Case "Horizontal"
    Return 933
Case "30 Degrees"
    Select Case drpDwnRoofFacing.Text
        Case "South"
            Return 1042
        Case "SE/SW"
            Return 997
        Case "E/W"
            Return 886
        Case "NE/NW"
            Return 762
        Case "North"
            Return 709
    End Select
Case "45 Degrees"
    Select Case drpDwnRoofFacing.Text
        Case "South"
            Return 1023
        Case "SE/SW"
            Return 968
        Case "E/W"
            Return 829
        Case "NE/NW"
            Return 666
        Case "North"
            Return 621
    End Select
Case "60 Degrees"
    Select Case drpDwnRoofFacing.Text
        Case "South"
            Return 960
        Case "SE/SW"
            Return 900
        Case "E/W"
            Return 753
        Case "NE/NW"
            Return 580
        Case "North"
            Return 485
    End Select
Case "Vertical"
    Select Case drpDwnRoofFacing.Text
        Case "South"
            Return 724
        Case "SE/SW"
            Return 684
        Case "E/W"
            Return 565
        Case "NE/NW"
            Return 427
        Case "North"
            Return 360
    End Select
End Select
于 2013-01-28T14:10:03.953 回答