[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 333
  • Last Modified:

Retrieve data from array

Idle_Mind,

Remember this code you did for me ?

The result is o ListBox is like:

600 400
300 250 100 50
etc..

but What If I would need the index of the amount ? (in an array or another list box doesn matter)
like that:


600 400                               4,7
300 250 100 50                   1,2,3,6

Like that I could know where was the amount. Becasue if there are two same amounts I cannot know wich it tooks...

any ideas?
Private Sub bgw_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles bgw.DoWork
        Dim values() As Decimal = DirectCast(e.Argument, Decimal())

        For i As Integer = 1 To values.Length
            Dim combos As Combinations(Of Decimal)

            combos = New Combinations(Of Decimal)(values, i)
            Dim matches = From combo In (From _set In combos Select _set) _
                          Where combo.Sum() = target _
                          Select combo

            For Each c As Object In matches
                solutions.Add(c.ToArray())
            Next

            For Each solution As Decimal() In solutions
                Dim str As String = ""

                For Each dec As Decimal In solution
                    str += dec.ToString() + " "
                Next
                AddTextToListBox(Trim(str))
                str = ""
            Next
            solutions.Clear()
        Next
    End Sub

Open in new window

0
PhilippeRenaud
Asked:
PhilippeRenaud
  • 4
  • 3
1 Solution
 
JuanCarnigliaCommented:
You want to know which element on the listbox has been selected?

{listboxname}.Index or SelectedIndex, or SelectedItem (returns the ListBoxItem object)

Greetings.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Hmmm...so if your original data was:

    private double[] values = { 200, 2000, 250, 10, 600, 500, 300, 400, 100};

And {600, 400} was a solution...you need to know that 600 was at Index 4?...and that 400 was at Index 7?
0
 
PhilippeRenaudAuthor Commented:
That's correct.  And if there were multiple solutions.. I would need for each of them the index..
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You can do something like below...

We are instead finding combinations of the Data Class which holds both the Decimal Value and its original Index:  
Imports Facet.Combinatorics
Public Class Form1

    Private target As Decimal = 1000
    Private values() As Decimal = {600, 200, 2000, 250, 10, 600, 500, 300, 400, 100}

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Not bgw.IsBusy Then
            Button1.Enabled = False
            ListBox1.Items.Clear()
            bgw.WorkerReportsProgress = True
            bgw.RunWorkerAsync(values)
        End If
    End Sub

    Private Sub bgw_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgw.DoWork
        Dim values() As Decimal = DirectCast(e.Argument, Decimal())

        Dim DataValues As New List(Of Data)
        For i As Integer = 0 To values.Count - 1
            Dim D As New Data
            D.Index = i
            D.Value = values(i)
            DataValues.Add(D)
        Next

        For i As Integer = 1 To DataValues.Count
            Dim combos As New Combinations(Of Data)(DataValues, i)
            Dim matches = From combo In (From _set In combos Select _set) _
                          Where combo.Sum(Function(x) x.Value) = target _
                          Select combo

            For Each solution As List(Of Data) In matches
                Dim dc As New DataCombination
                dc.DataValues = solution
                bgw.ReportProgress(-1, dc)
            Next
        Next
    End Sub

    Private Sub bgw_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles bgw.ProgressChanged
        ListBox1.Items.Add(e.UserState)
    End Sub

    Private Sub bgw_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgw.RunWorkerCompleted
        Button1.Enabled = True
        MessageBox.Show("Done!")
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        If ListBox1.SelectedIndex <> -1 Then
            Dim dc As DataCombination = ListBox1.SelectedItem
            Debug.Print("Solution:")
            For Each D As Data In dc.DataValues
                Debug.Print(D.Value & " @ Index " & D.Index)
            Next
        End If
    End Sub

End Class

Public Class Data

    Public Index As Integer
    Public Value As Decimal

    Public Overrides Function ToString() As String
        Return Value.ToString
    End Function

End Class

Public Class DataCombination

    Public DataValues As New List(Of Data)

    Public Overrides Function ToString() As String
        Dim sb As New System.Text.StringBuilder
        For Each D As Data In DataValues
            sb.Append(D.ToString & " ")
        Next
        Return sb.ToString.Trim
    End Function

End Class

Open in new window



Idle-Mind-445783.flv
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
*Note that the LINQ query has changed to:  
Dim matches = From combo In (From _set In combos Select _set) _
                          Where combo.Sum(Function(x) x.Value) = target _
                          Select combo

Open in new window


...so that we are getting the sum of the Value() member of our Data class.
0
 
PhilippeRenaudAuthor Commented:
Yes perfect... thanks a lot Idle!
0
 
PhilippeRenaudAuthor Commented:
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now