Search for equalities in VB.NET

1

There are 8 outputs and each of them, is identified with a letter from A to H . Each output will have a variable ( TextBox ) where the user will only put numbers and when they finish adding the numbers, they want to make an operation:

Ideally, in all exits have a different number.

For example:

Salida      A   B   C   D   E   F   G   H
variable    1   2   3   4   5   6   7   8

But the problem is generated when there are equal numbers.

Salida      A   B   C   D   E   F   G   H
variable    1   1   2   3   4   5   6   6

For Visual Studio it is necessary to count the equalities in this case 2 , save this number and project and identify the outputs that are equal, in this case A B , G H and expose them.

The variables are TextBox , and the outputs are labels .

Example 2:

Salida      A   B   C   D   E   F   G   H
variable    4   3   2   3   4   5   5   6
  

equalities = 3
  Equal outputs: AD, BD, FG

Code VB.NET (what I tried to do):

Public Class Form1
    Dim iguales As Integer
    Private txt As String
    Dim i As Integer
    Dim u As Integer
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        iguales = 0
        txt = ""

        For i = 1 To 7
            For u = i + 1 To 8
                If Me.Controls("TextBox" & i.ToString).Text = Me.Controls("TextBox" & u.ToString).Text Then
                   // ' U es igual I
                    iguales = iguales + 1
                    txt = txt + u + i + ", "
                End If
            Next
        Next
        txt = RichTextBox1.Text
        iguales = RichTextBox2.Text
   End Sub
End Class
    
asked by Felipe Arriagada 07.05.2017 в 04:27
source

2 answers

1

I would use the GroupBy function of LINQ. If you're not familiar with LINQ, it might seem a bit cryptic, but it's worth learning.

For example, in your case, you can achieve everything you want in a few lines of code:

Dim valores() = {
    Tuple.Create("a", TextBox1.Text),
    Tuple.Create("b", TextBox2.Text),
    Tuple.Create("c", TextBox3.Text),
    Tuple.Create("d", TextBox4.Text),
    Tuple.Create("e", TextBox5.Text),
    Tuple.Create("f", TextBox6.Text),
    Tuple.Create("g", TextBox7.Text),
    Tuple.Create("h", TextBox8.Text)
}

Dim igualdades = valores _
    .GroupBy(Function(v) v.Item2) _
    .Where(Function(g) g.Count > 1) _
    .ToList

RichTextBox1.Text = igualdades.Count
RichTextBox2.Text = String.Join(", ", igualdades.Select(Function(g) String.Join(String.Empty, g.Select(Function(i) i.Item1))))

Although it would not be reasonable to leave you a complete tutorial on how the code works, at least I can explain the key parts.

With ...

.GroupBy(Function(v) v.Item2)

... identical groups of values are created, and with ...

.Where(Function(g) g.Count > 1)

... the list of groups is filtered to those that contain more than one element. These are the equalities you are looking for.

Then ...

igualdades.Count

... gives you the amount of equalities.

And finally, with a combination of calls to String.Join() you can create the list of equals separated by commas.

    
answered by 08.05.2017 / 04:38
source
0

A great friend helped me ... but we are still looking for ways to identify the letters that are the same:

Public Class Form1
Dim iguales As Integer
Private txt As String
Dim i As Integer
Dim u As Integer
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim valores() As String = {TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text,
    TextBox5.Text, TextBox6.Text, TextBox7.Text, TextBox8.Text}

    Dim letras() As String = {"a", "b", "c", "d", "e", "f", "g", "h"}

    Dim igualdad As String = ""
    Dim cantigualdad As Integer = 0
    For i As Integer = 0 To 7
        For j As Integer = 0 To 7
            If letras(i) <> letras(j) Then
                If valores(i).ToString = valores(j).ToString Then
                    igualdad = igualdad & letras(i).ToString & letras(j).ToString
                    cantigualdad = cantigualdad + 1
                End If
            End If
        Next
    Next

    RichTextBox1.Text = cantigualdad.ToString() / 2




End Sub

End Class

    
answered by 08.05.2017 в 03:47