2016-06-09 24 views
1

我正在創建一個輸出排序函數。用戶需要能夠頻繁地更改訂單(它用於爲我們的訂單選擇器訂購選擇列表)。我創建了一個Windows窗體,但我需要對它進行一些驗證。驗證本身很簡單...數字必須是有序的,不能重複。如果他們重複一遍,SQL報告會彈出...我還想驗證不跳過任何值,但這並不是必需的。用戶在數字列表中分配不重複的值

有沒有更好的辦法比:

if NumericUpDown1.value = NumericUpDown2.value then 
    error goes here 
end if 
if NumericUpDown1.value = NumericUpDown3.value then 
    error goes here 
end if 
if NumericUpDown1.value = NumericUpDown4.value then 
    error goes here 
end if ... 

有一個大名單,而這將是上千行的代碼。我知道必須有一個簡單的解決方案。這不是我來的,我一直堅持了幾天。 (是的,我知道我可能已經做到了)

+0

將控件放入集合中(手動或通過查詢表單控件的特定類型),並通過將它們分組到「值」來檢查重複項。 –

+0

創建一個'NumericUpDownN.value'數組並對其進行排序,並遍歷數組以找到2個相等的i,i + 1值。不知道VB是否支持linq,即使不是,也不會有太多的代碼。 – Serg

回答

1

就像Serg說的那樣,把值放入一個數組中,並使用Linq按值進行分組。然後找到任何重複。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 

    Dim numericList = New Decimal() { 
      NumericUpDown1.Value, 
      NumericUpDown2.Value, 
      NumericUpDown3.Value, 
      NumericUpDown4.Value, 
      NumericUpDown5.Value 
     } 

    Dim duplicatesExist = numericList _ 
     .GroupBy(Function(n) n) _ 
     .Any(Function(g) g.Count() > 1) 

    If duplicatesExist Then 
     MessageBox.Show("Duplicates exist") 
    Else 
     MessageBox.Show("No Duplicates") 
    End If 

End Sub 
+0

非常感謝。當我的回答非常明顯時,我討厭它,但它並不會來到你的身邊......我處於一個我沒有任何支持或者任何人都沒有意見的位置。 –

+0

它解決你的問題嗎?如果是,請接受我的回答。如果您需要支持或想法,您可以在此網站中創建新主題。 – Han

+0

不好意思,我做過了,但它一開始並沒有... –