Results 1 to 7 of 7

Thread: [RESOLVED] Counting elements in table

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2014
    Posts
    10

    Resolved [RESOLVED] Counting elements in table

    HI

    I need some help. I have two arrays. First one contains elements and the second one names. Something like this:

    array1 array2
    element1 jack
    element2 peter
    element3 sue
    element2 marc
    element1 nicky
    element2 liam
    element3 emily

    I'd like to create table like this:

    element1: jack, nicky
    element2: peter, marc, liam
    element3: sue, emily

    I have no idea where to start...

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,439

    Re: Counting elements in table

    What do you mean by "a table"? It's too general a term in this context I'm afraid. Do you mean a database table? A DataTable? A TableLayoutPanel? A generalised data structure? A tabular UI display? Something else?

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2014
    Posts
    10

    Re: Counting elements in table

    Actualy, it doesn't matter. It could be TableLayoutPanel.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,439

    Re: Counting elements in table

    Quote Originally Posted by najki View Post
    Actualy, it doesn't matter. It could be TableLayoutPanel.
    Actually, it does matter because the specifics of how you do it would be different depending on what you actually want to achieve. Here's some code that will allow you to turn those two arrays into a Dictionary(Of String, List(Of String)). I'll leave it to you to decide what to do with it from there:
    Code:
    Dim data As New Dictionary(Of String, List(Of String))
    
    For i = 0 To array1.GetUpperBound(0)
        Dim key = array1(i)
        Dim value As List(Of String)
    
        If data.ContainsKey(key) Then
            value = data(key)
        Else
            value = New List(Of String)
            data.Add(key, value)
        End If
    
        value.Add(array2(i))
    Next
    
    For Each key As String In data.Keys
        Console.WriteLine("{0}: {1}", key, String.Join(",", data(key)))
    Next

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2014
    Posts
    10

    Re: Counting elements in table

    Sorry for long reply. I just want to display this table. Nothing else.

    When I use your code an error occurs: Value of type 'System.Collections.Generic.List(Of String)' cannot be converted to '1-dimensional array of String'.

    I don't know how to fix that. I'm pretty new to programming and I 've never worked with Dictionary.

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,439

    Re: Counting elements in table

    If you're using .NET 3.5 or earlier then call ToArray on the List to convert it to an array.

  7. #7

    Thread Starter
    New Member
    Join Date
    Aug 2014
    Posts
    10

    Re: Counting elements in table

    Thank you for your answers.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width