And whai in case if you have numbers:
2,2,2,3,3,3,4
Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474
I did a code, but according to your last example this is not just it:
Take a look:
Public Sub New()
InitializeComponent()
listBox1.Items.AddRange(New Object() {2, 2, 3, 5, 5, 7})
End Sub
Private Sub button1_Click(sender As Object, e As EventArgs)
Dim list As New List(Of Integer)()
For i As Integer = 0 To listBox1.Items.Count - 1
Dim value1 As Integer = Integer.Parse(listBox1.Items(i).ToString())
For j As Integer = i + 1 To listBox1.Items.Count - 1
Dim value2 As Integer = Integer.Parse(listBox1.Items(j).ToString())
If value1 = value2 Then
list.Add(value1)
RemoveItems(value1)
End If
Next
Next
'for the end, you do the math operations over the values:
Dim result1 As Integer = 1
For Each number As Integer In list
result1 *= number
Next
Dim result2 As Integer = 1
For i As Integer = 0 To listBox1.Items.Count - 1
result2 *= Integer.Parse(listBox1.Items(i).ToString())
Next
MessageBox.Show("Variable 1 = " & result1 & vbLf & "Variable 2 = " & result2)
End Sub
Private Sub RemoveItems(value As Integer)
For i As Integer = 0 To listBox1.Items.Count - 1
listBox1.Items.Remove(value)
Next
End Sub
Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474
2 - (Removed)
2 - (Removed)
2
3 - (Removed)
3 - (Removed)
3
4
If I understood, it should be like that:
Variable1 = 2 * 3
Variable2 = 3 (only 3 left)
----------------------------------
In your upper example there were 3 and 7 left, and you multiplay them and got 21!
Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474
O`right, this will do it:
Public Sub New()
InitializeComponent()
listBox1.Items.AddRange(New Object() {2, 2, 2, 3, 13})
End Sub
Private Sub button2_Click(sender As Object, e As EventArgs)
Dim list As New List(Of Integer)()
For i As Integer = 0 To listBox1.Items.Count - 1
Dim value1 As Integer = Integer.Parse(listBox1.Items(i).ToString())
For j As Integer = i + 1 To listBox1.Items.Count - 1
Dim value2 As Integer = Integer.Parse(listBox1.Items(j).ToString())
If value1 = value2 Then
list.Add(value1)
RemoveItems(value1, i, j)
End If
Next
Next
'for the end, you do the math operations over the values:
Dim result1 As Integer = 1
For Each number As Integer In list
result1 *= number
Next
Dim result2 As Integer = 1
For i As Integer = 0 To listBox1.Items.Count - 1
result2 *= Integer.Parse(listBox1.Items(i).ToString())
Next
MessageBox.Show("Variable 1 = " & result1 & vbLf & "Variable 2 = " & result2)
End Sub
Private Sub RemoveItems(value As Integer, a As Integer, b As Integer)
For i As Integer = 0 To listBox1.Items.Count - 1
If i = a Then
listBox1.Items.RemoveAt(a)
ElseIf i = b Then
listBox1.Items.RemoveAt(b)
End If
Next
End Sub
Let me know if its working (it does here).
Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474