We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Retrieve data from array

Medium Priority
342 Views
Last Modified: 2012-05-11
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

Comment
Watch Question

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

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

Greetings.
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
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?

Author

Commented:
That's correct.  And if there were multiple solutions.. I would need for each of them the index..
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
*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.

Author

Commented:
Yes perfect... thanks a lot Idle!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.