Link to home
Start Free TrialLog in
Avatar of Philippe Renaud
Philippe RenaudFlag for Canada

asked on

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

Avatar of JuanCarniglia
JuanCarniglia
Flag of Argentina image

You want to know which element on the listbox has been selected?

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

Greetings.
Avatar of Mike Tomlinson
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?
Avatar of Philippe Renaud

ASKER

That's correct.  And if there were multiple solutions.. I would need for each of them the index..
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
*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.
Yes perfect... thanks a lot Idle!