CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2007
    Posts
    56

    VS 2005 - Help displaying contents of ArrayList

    Hello all,

    I am having a problem displaying a custom array list I have in my code. (BTW. It is 1am where I am. I have been at this for a long time so I apologize if I don't make a lot of sense).

    I built an arraylist of objects from a custom class. The arraylist contains 2 columns\dimensions.
    The first is type string, the second decimal.

    I am now attempting to take my arraylist and display it in my form. I have tried using a listview and iterating through it, but unsure of how to split the two columns up. I have tried something similar to the following I found in another forum:

    Code:
                For i As Integer = 0 To arSearchArray.Count - 1
                    Dim item As String = CType(arSearchArray(i), String)
                    Dim arrItem() = Split(item, "___")
                    Dim url As String = arrItem(0)
                    Dim weight As String = arrItem(1)
                    Dim lstItem As New ListViewItem(url, 0)
                    lstItem.SubItems.Add(weight)
                    lstSearchResults.Items.Add(lstItem)
    
                Next
    but of course because the arraylist is a custom list it doesn't like me trying to cast it as a string. Plus I don't know if my split delimiter would work. Again, that is not my code, but code I found on another site.

    I then tried a datagridview. But unsure about how to proceed. I created my datagridview on my form, but when I bind nothing happens. I know I am missing something as I am sure I will need more than just

    Code:
    dgSearchResults.DataSource = arSearchArray
    however, my brain has decided to shut down now and so I will see if someone else much smarter\experienced then me can help out.

    Please don't suggest I try and store my data in something else than the arraylist as I have written a lot of code around this including custom sorting code and going back to the drawing board is not an option (this is for a school project due soon).


    Thanks again in advance.

  2. #2
    Join Date
    Mar 2007
    Posts
    56

    Re: VS 2005 - Help displaying contents of ArrayList

    Sorry this may be a long post, but I appreciate your patience with my descriptions of my problem.

    Hi so after a couple of hours of sleep I am back at it again...sigh.

    I know how to put a Dataset into a listview so I thought hey, let's convert the ArrayLIst to a Dataset and go from there. Not sure if I am on the right track but here is what I tried.

    Code:
            Dim arSearchArray As ArrayList
            Dim var As CustomArray
            Dim dsArraySet As New DataSet
            Dim dtArrayTable As New DataTable
            Dim drArrayRow As DataRow
    
                dsArraySet.Tables.Add(dtArrayTable)
                dsArraySet.Tables(0).Columns.Add("URL", GetType(String))
                dsArraySet.Tables(0).Columns.Add("Term Weight", GetType(Decimal))
    
                For i As Integer = 0 To arSearchArray.Count - 1
                    For Each var In arSearchArray(i)                               '<-- Breaking Here!!!!
                        drArrayRow = dsArraySet.Tables(0).NewRow
                        drArrayRow(0) = var
                        dsArraySet.Tables(0).Rows.Add(drArrayRow)
                    Next
                Next
    When it gets to the point I have indicated above I receive the error: Unable to cast object of type 'URL_Search.CustomArray' to type 'System.Collections.IEnumerable'

    A little bit more background on my application. This is an Information Retrieval project for my Advanced Database class at University. I have a DB that contains webpage urls and the words in their body.

    The user searches for a word, the app queries the DB for URLs that contain the word. The arraylist then is populated with the URL and the TermWeight of that URL (Vector Space Model).

    In order to search the arraylist I built a custom array class. Here is the code for the class:

    Code:
    Public Class CustomArray
        Implements IComparable
    
        Public URL As String
        Public termWeight As Decimal
    
        Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo
            If Not TypeOf obj Is CustomArray Then
                Throw New Exception("This is not a correct type of object for the array")
            End If
    
            Dim Compare As CustomArray = CType(obj, CustomArray)
            Dim result As Integer = Me.termWeight.CompareTo(Compare.termWeight)
    
            If result = 0 Then
                result = Me.termWeight.CompareTo(Compare.termWeight)
            End If
    
            Return result
    
        End Function
    End Class
    Here is how the arraylist is populated:
    Code:
    'NOTE above this code is a call to the DB to get the URLs that the search term is located in
                Do While sqlReader.Read
                    Dim arrObject As CustomArray = New CustomArray
    
                    arrObject.URL = sqlReader("address")
    
                    'Need to go and calculate the Vector and put it into the array
                    tmpTermWeight = getVector(arrObject.URL.ToString)
                    arrObject.termWeight = tmpTermWeight
                    arBuildArray.Add(arrObject)
                Loop
    So farther down I sort the arraylist based on the termweight calculated.

    So I now have this arraylist based on a custom class. I need to get it to my form.

    What are your suggestions? Bind to DatagridView? ListView? Some other method?

    I appreciate your help and apologize for this long post.


    Dave

  3. #3
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: VS 2005 - Help displaying contents of ArrayList

    I think that LINQ would help you greatly.

    http://msdn.microsoft.com/en-us/vbasic/bb688088.aspx
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

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

Featured