dipa
dipa

Reputation: 197

Array Loop in vb.net

comdata(0, 0) = "5"
comdata(0, 1) = "3"
comdata(0, 2) = "10"
comdata(0, 3) = Nothing
comdata(1, 0) = "1"
comdata(1, 1) = "7"
comdata(1, 2) = "14"
comdata(1, 3) = Nothing
comdata(2, 0) = "5"
comdata(2, 1) = "8"
comdata(2, 2) = "14"
comdata(2, 3) = Nothing

I have an array that looks like above. I would like to make something like below. The idea is when there are same value on the array comdata(,), for example there are two value 5, on the comdata(0, 0) and comdata(2, 0). I would like to set value on the comdata(2, 0).

choose(0,0) = True  
choose(0,1) = True  
choose(0,2) = True  
choose(0,3) = False
choose(1,0) = True  
choose(1,1) = True
choose(1,2) = True  
choose(1,3) = False
choose(2,0) = False 
choose(2,1) = True  
choose(2,2) = False 
choose(2,3) = False 

I have try it with the code below, unfortunately when the value of i = 2, it cannot check the comdata(0, 0), comdata(0, 1), comdata(0, 2), comdata(0, 3). So the value of choose(0, 0), choose(0, 1), choose(0, 2) will be set to true.

For i = 0 To 2
    For j = 0 To 2
        If comdata(i, j) <> Nothing Then
            If i = 0 Then
                choose(i, j) = True
            Else
                For k = 0 To 2
                    If comdata(i, j) = comdata(i - 1, k) Then
                        choose(i, j) = False
                    Else
                        choose(i, j) = True
                    End If
                Next
            End If
        End If
    Next
Next

Upvotes: 0

Views: 431

Answers (2)

the_lotus
the_lotus

Reputation: 12748

It's a bit confusing what you are trying to do. When you loop for "k" you only look for values inside "i" you need to check all values. You're also only looping to 2 when your array can definetly go to 3.

    For x As Integer = 0 To 2
        For y As Integer = 0 To 3
            If x = 0 And y = 0 Then
                choose(x, y) = True
            ElseIf comdata(x, y) IsNot Nothing Then
                choose(x, y) = True

                For u As Integer = 0 To 2
                    For v As Integer = 0 To 3
                        If x = u And y = v Then
                            Exit For
                        End If

                        If comdata(x, y) = comdata(u, v) Then
                            choose(x, y) = False
                        End If
                    Next

                    If u = x Then
                        Exit For
                    End If
                Next
            End If
        Next
    Next

Upvotes: 1

Dan Puzey
Dan Puzey

Reputation: 34198

Some of your code doesn't make sense to me: I don't see what the purpose of If i = 0 Then ... is. This has the effect of setting your first three elements to True even though two of them have no duplicate. Ignoring that for now and carrying on...

You're trying to do this with three loops, but you need to use four: two outer loops (i and j define the current element being compared), and two inner loops (k and a new loop that define the element being compared to).

So, where you're currently using comdata(i - 1, k), you instead need a further loop (For l = 0 To 2) and replace i - 1 with l.

Note that you will need a further guard clause in this case, because at some point in your loop you will compare your element to itself. That is, if i = l and j = k then you should not make the comparison - otherwise each element will treat itself as a duplicate.

Upvotes: 0

Related Questions