我可以简化这个VB.net代码,所以我不必重复这么多吗?

时间:2012-01-31 14:33:14

标签: vb.net

我正在创建一个调度算法。我的代码变得越来越长。任何人都可以建议我如何缩短我的代码,因为我必须在下面多次重复同样的事情,而外部的其他是4或5我还要重复它:

以下是我的部分代码的示例:

 ElseIf Val(jobs_txt.Text) = 3 Then
            If (a < b And b < c) Then
                awt1_lbl.Text = Val(0)
                awt2_lbl.Text = a
                awt3_lbl.Text = a + b
                y = (Val(awt1_lbl.Text) + Val(awt2_lbl.Text) + Val(awt3_lbl.Text)) / 3
                awt_ans.Text = y
            ElseIf (a < c And c < b) Then
                awt1_lbl.Text = Val(0)
                awt2_lbl.Text = a + b
                awt3_lbl.Text = a
                y = (Val(awt1_lbl.Text) + Val(awt2_lbl.Text) + Val(awt3_lbl.Text)) / 3
                awt_ans.Text = y
            ElseIf (b < a And a < c) Then
                awt1_lbl.Text = a
                awt2_lbl.Text = Val(0)
                awt3_lbl.Text = a + b
                y = (Val(awt1_lbl.Text) + Val(awt2_lbl.Text) + Val(awt3_lbl.Text)) / 3
                awt_ans.Text = y
            ElseIf (b < c And c < a) Then
                awt1_lbl.Text = a + b
                awt2_lbl.Text = Val(0)
                awt3_lbl.Text = a
                y = (Val(awt1_lbl.Text) + Val(awt2_lbl.Text) + Val(awt3_lbl.Text)) / 3
                awt_ans.Text = y
            ElseIf (c < a And a < b) Then
                awt1_lbl.Text = a
                awt2_lbl.Text = a + b
                awt3_lbl.Text = Val(0)
                y = (Val(awt1_lbl.Text) + Val(awt2_lbl.Text) + Val(awt3_lbl.Text)) / 3
                awt_ans.Text = y
            ElseIf (c < b And b < a) Then
                awt1_lbl.Text = a + b
                awt2_lbl.Text = a
                awt3_lbl.Text = Val(0)
                y = (Val(awt1_lbl.Text) + Val(awt2_lbl.Text) + Val(awt3_lbl.Text)) / 3
                awt_ans.Text = y
            End If

4 个答案:

答案 0 :(得分:3)

是的,你可以做几件事:

  • y的计算在每种情况下都是相同的,因此您无需重复。
  • 最后ElseIf将始终为真,因此请使用Else
  • 使用Val(0)会导致大量隐式转化,实际上最终会成为CStr(Val(Cstr(0))),所以只需使用"0"

代码:

ElseIf Val(jobs_txt.Text) = 3 Then
  If (a < b And b < c) Then
    awt1_lbl.Text = "0"
    awt2_lbl.Text = a
    awt3_lbl.Text = a + b
  ElseIf (a < c And c < b) Then
    awt1_lbl.Text = "0"
    awt2_lbl.Text = a + b
    awt3_lbl.Text = a
  ElseIf (b < a And a < c) Then
    awt1_lbl.Text = a
    awt2_lbl.Text = "0"
    awt3_lbl.Text = a + b
  ElseIf (b < c And c < a) Then
    awt1_lbl.Text = a + b
    awt2_lbl.Text = "0"
    awt3_lbl.Text = a
  ElseIf (c < a And a < b) Then
    awt1_lbl.Text = a
    awt2_lbl.Text = a + b
    awt3_lbl.Text = "0"
  Else
    awt1_lbl.Text = a + b
    awt2_lbl.Text = a
    awt3_lbl.Text = "0"
  End If
  y = (Val(awt1_lbl.Text) + Val(awt2_lbl.Text) + Val(awt3_lbl.Text)) / 3
  awt_ans.Text = y

实际上,如果不必将值转换为字符串然后再转换为数字,则可以像这样简单地计算y

y = (a * 2 + b) / 3

答案 1 :(得分:3)

首先,看起来你可以移动最后两行

y = (Val(awt1_lbl.Text) + Val(awt2_lbl.Text) + Val(awt3_lbl.Text)) / 3
awt_ans.Text = y

if-then-else之外,因为它们在所有六种情况下都做同样的事情。

接下来,既然您的代码看起来像Val(0)分配给具有最小密钥的项目,a+b分配给中间的值,a分配给最大的密钥,您可以放置键数组中的abcawt1_lblawt2_lbawt3_lbl成为值数组,并排序他们,像这样:

Dim keys() As String = { a, b, c }
Dim labels() As LabelType = { awt1_lbl, awt2_lb, awt3_lbl } // put real label type there
Array.Sort(keys, labels)
labels(0).Text = Val(0)
labels(1).Text = a+b
labels(2).Text = a

答案 2 :(得分:1)

你应该创建一个类,然后它更容易阅读,重用维护和扩展:

Dim awt = New AWT(a, b, c)
awt1_lbl.Text = awt.GetAWT1().ToString
awt2_lbl.Text = awt.GetAWT2().ToString
awt3_lbl.Text = awt.GetAWT3().ToString
awt_ans.Text  = awt.GetY().ToString

这是一个可能的实现,我已经使它成为Option Strict,因此它可以很容易地转换为C#,并且不易出错。

Public Class AWT
    Public Property A As Int32
    Public Property B As Int32
    Public Property C As Int32

    Public Sub New(a As Int32, b As Int32, c As Int32)
        Me.A = a
        Me.B = b
        Me.C = c
    End Sub

    Public Function GetY() As Int32
        Return GetAWT1() + GetAWT2() + GetAWT3()
    End Function

    Public Function GetAWT1() As Int32
        If (A < B AndAlso B < C) OrElse (A < C AndAlso C < B) Then
            Return 0
        ElseIf (B < A AndAlso A < C) OrElse (C < A AndAlso A < B) Then
            Return A
        Else
            Return A + B
        End If
    End Function

    Public Function GetAWT2() As Int32
        If (A < B AndAlso B < C) OrElse (C < B AndAlso B < A) Then
            Return A
        ElseIf (A < C AndAlso C < B) OrElse (C < A AndAlso A < B) Then
            Return A + B
        Else 
            Return 0
        End If
    End Function

    Public Function GetAWT3() As Int32
        If (A < B AndAlso B < C) OrElse (B < A AndAlso A < C) Then
            Return A + B
        ElseIf (A < C AndAlso C < B) OrElse (B < C AndAlso C < A) Then
            Return A
        Else
            Return 0
        End If
    End Function

End Class

答案 3 :(得分:-1)

除了其他答案外,还有:

If (a < b And b < c) Then

应与:

相同
If (a < b < c) Then

为您节省几个字符!

编辑:MetaKnight是对的,这是无效的语法。然而编译器没有抱怨,我无法弄清楚它的作用。