Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 减少VB.NET中if代码的数量_Asp.net_Vb.net_Web - Fatal编程技术网

Asp.net 减少VB.NET中if代码的数量

Asp.net 减少VB.NET中if代码的数量,asp.net,vb.net,web,Asp.net,Vb.net,Web,我正在使用ASP Web窗体和VB后端创建一个太阳能光伏计算器。我只是看看我的代码,作为一名VB.NET开发新手,我只是想知道是否有一种方法可以做一些不同的事情,减少代码行数或类似的事情。下面是y代码中一个麻烦的部分: Dim SR As Integer 'Store radiation value If drpDwnRoofFacing.Text = "South" And drpAngleOfRoof.Text = "Horizontal" Then SR =

我正在使用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

您可以嵌套IF语句,即


如果drpAngleOfRoof.Text=“水平”,则

如果结束

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

或者您可以使用Select…Case语句作为替代


我建议将这些数据存储在应用程序代码之外,例如存储在数据库中,或者如果不需要,存储在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))
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
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
然后,您可以硬编码规则列表的创建,就像它们来自数据库一样,如下所示:

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))
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
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
然后,您可以使用一个简单的循环来应用规则,而不是一个巨大的
If
语句,如下所示:

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))
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
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
正如您所看到的,一旦您以这种方式进行设计,以后修改规则或添加其他规则将更加容易。但是,您应该对这些值使用枚举,而不是字符串。例如:

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

Public Enum AnglesOfRoof
    Any
    Horizontal
    Degrees30
    Degrees45
    ' ...
    Vertical
End Enum

改用select语句怎么样。您将把最初的选择削减到drpAngleOfRoof的值,然后只需要担心drpdownroof的值。大概是这样的:

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))
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
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

更好的方法是使用
Select Case
。非常感谢,当然!选择案例,与if相比有什么好处?哇,太棒了,我喜欢你们的知识。我真的很感激。我觉得通过问这个问题,我学到了比无数个小时搜索MSDN阅读和学习每个部分更多的东西。