Question:
Check Duplicate Value in Array VB.Net?
?
2012-12-06 10:42:57 UTC
Hi,

how can i check if there's a duplicate value in array?

Dim array(9) as String

array(0) = Textbox1.Text
array(1) = Textbox2.Text
array(2) = Textbox3.Text
array(3) = Textbox4.Text
array(4) = Textbox5.Text
array(5) = Textbox6.Text
array(6) = Textbox7.Text
array(7) = Textbox8.Text
array(8) = Textbox9.Text
array(9) = Textbox10.Text

if there's a duplicate value or text then the backcolor of the textbox turns to red.


any help will be highly appreciated.
Four answers:
texasmaverick
2012-12-06 12:51:34 UTC
The following code works well.





Public Class Form1



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

Dim Num1 As Integer

Dim array(9) As String



array(0) = Textbox1.Text

array(1) = Textbox2.Text

array(2) = Textbox3.Text

array(3) = Textbox4.Text

array(4) = Textbox5.Text

array(5) = Textbox6.Text

array(6) = Textbox7.Text

array(7) = Textbox8.Text

array(8) = Textbox9.Text

array(9) = TextBox10.Text



For I = 0 To array.Length - 2

For j = I + 1 To array.Length - 1

If array(j) = array(I) Then

Num1 = j + 1

If Num1 = 1 Then

TextBox1.BackColor = Color.Red

ElseIf Num1 = 2 Then

TextBox2.BackColor = Color.Red

ElseIf Num1 = 3 Then

TextBox3.BackColor = Color.Red

ElseIf Num1 = 4 Then

TextBox4.BackColor = Color.Red

ElseIf Num1 = 5 Then

TextBox5.BackColor = Color.Red

ElseIf Num1 = 6 Then

TextBox6.BackColor = Color.Red

ElseIf Num1 = 7 Then

TextBox7.BackColor = Color.Red

ElseIf Num1 = 8 Then

TextBox8.BackColor = Color.Red

ElseIf Num1 = 9 Then

TextBox9.BackColor = Color.Red

ElseIf Num1 = 10 Then

TextBox10.BackColor = Color.Red

End If

End If

Next

Next



End Sub

End Class





TexMav
?
2016-09-30 17:08:13 UTC
Vb.net String Array
Mr Ed
2012-12-06 12:40:55 UTC
You have to loop through the entire array, checking each 'slot' to see if the value is the same as any other value.



For example, array(3) might be the same value as array(9).



For this a double FOR-NEXT loop works nicely



for x = 0 to max_array

.....for y = 0 to max_array

..........if x <> y then

...............if array(x) = array(y) then

....................' we have a duplicate

....................' duplicate code goes here

...............end if

.........end if

.....next y

next x



This loop looks at the value in array(0) and then checks it against all others. Then, it looks at the value in array(1) and checks that against all others. Etc.
?
2012-12-06 11:17:14 UTC
If array.Contains(Textbox1.Text) Then

' Entry already exists

End If


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Search results for 'Check Duplicate Value in Array VB.Net?' (Questions and Answers)
4
replies
acessing cached pages?
started 2006-07-07 05:56:14 UTC
computers & internet
Loading...