我正在使用ASP Web Forms和VB后端创建一个太阳能PV计算器。我只是看了我的代码并成为一名新手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
答案 0 :(得分:4)
我建议将这些数据存储在应用程序代码之外,例如在数据库中,或者如果不需要,则存储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
答案 1 :(得分: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
答案 2 :(得分:2)
您可以嵌套IF语句,即
If drpAngleOfRoof.Text = "Horizontal" Then
If drpDwnRoofFacing.Text = "South" Then
SR = 933
End If
If drpDwnRoofFacing.Text = "South" Then
SR = 933
End If
将所有与每个角度位置相关的陈述组合在一起,例如。
或者您可以使用Select ... Case语句作为替代方法。
http://msdn.microsoft.com/en-us/library/cy37t14y(v=vs.71).aspx
答案 3 :(得分:-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