gpt4 book ai didi

asp.net - 减少 VB.NET 的 if 代码量

转载 作者:行者123 更新时间:2023-12-02 02:05:54 25 4
gpt4 key购买 nike

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

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

最佳答案

我建议将此数据存储在应用程序代码之外,例如数据库中,或者如果没有必要,则存储在 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

关于asp.net - 减少 VB.NET 的 if 代码量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14563463/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com