• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 306
  • Last Modified:

Problem returning generic List(Of T) object

I've run into a problem that I cannot seem to pin down.  I have a custom object (created as a structure).  For example purposes, I'll call my object ItemDetails here.  In the course of my application, I do a check to see if the object I'm trying to update already exists in my database.
            Dim UpdatedItem As ItemDetails  
            Dim ExistingItems As New List(Of ItemDetails)  
  
            For Each DatabaseRow As DataRow In DatabaseTable.Rows  
                UpdatedItem = New ItemDetails  
  
                UpdatedItem = GetItemFromDatabase(DatabaseRow)  
  
                ExistingItems = GetExistingItemDetail(UpdatedItem)  
  
                If Not IsNothing(ExistingItems) Then  
                    For I As Integer = 0 To ExistingItems.Count  
                        UpdateItemData(UpdatedItem, ExistingItems(I))  
                    Next  
                Else  
                    InsertNewRecord(UpdatedItem)  
                End If  
  
                UpdatedItem = Nothing  
  
                Application.DoEvents()  
            Next DatabaseRow

Open in new window

To check this, I call a function that is supposed to return a list of my ItemDetails object (GetExistingItemDetail).  When I don't find a matching record, the function is written to return a value of Nothing.  These calls to the function work exactly as expected.

However, when I actually find a duplicate record in the database, the function won't return a valid List(Of ItemDetails).
    Private Function GetExistingItemDetail(ByVal UpdatedItem As ItemDetails) As List(Of ItemDetails) 
        Dim FoundItems As New List(Of ItemDetails) 
        Dim DatabaseItems As New DataTable 
        Dim ExistingAdapter As New OleDb.OleDbDataAdapter 
 
        SQLStr = "SELECT * FROM Items " _ 
                & "WHERE ItemID = '" & UpdatedItem.ID & "'" 
 
        Try 
            ExistingAdapter = New OleDb.OleDbDataAdapter(SQLStr, GAPConn) 
 
            DBConn.Open() 
            ExistingAdapter.Fill(DatabaseItems) 
        Catch DBException As Exception 
            MsgBox("There was a problem retrieving the data for item " & UpdatedItem.ID & vbCrLf _ 
                   & DBException.Message, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "DATABASE ERROR") 
 
            FoundItems = Nothing 
        Finally 
            ExistingAdapter = Nothing 
            DBConn.Close() 
        End Try 
 
        ' ********************************************************************* 
        ' ** If a database error occurred above, return nothing and exit the ** 
        ' ** function to prevent further execution in this function.         ** 
        ' ********************************************************************* 
        If IsNothing(DatabaseItems) Then 
            FoundItems = Nothing 
 
            Return FoundItems 
            Exit Function 
        End If 
 
        ' ********************************************************************* 
        ' ** If no loans were found, return nothing and exit the function.   ** 
        ' ********************************************************************* 
        If DatabaseItems.Rows.Count = 0 Then 
            FoundItems = Nothing 
            DatabaseItems = Nothing 
 
            Return FoundItems 
            Exit Function 
        End If 
 
        Try 
            For I As Long = 0 To DatabaseItems.Rows.Count - 1 
                Dim Item As New ItemDetails 
                Dim CurrentItem As DataRow 
 
                CurrentItem = DatabaseItems.Rows(I) 
 
                ' ********************************************************************* 
                ' **Here's where the Item object is populated. 
                ' ********************************************************************* 
 
                I = I + 1 
 
                ' ********************************************************************* 
                ' ** Add the waiver we just populated to the array list, reset the   ** 
                ' ** temporary waiver object and check for the next record.          ** 
                ' ********************************************************************* 
                FoundItems.Add(Item) 
                Item = Nothing 
                CurrentItem = Nothing 
            Next I 
        Catch DBException As OleDb.OleDbException 
            MsgBox("An error occurred retrieving the data for item " & UpdatedItem.ID) 
        End Try 
 
        Return FoundItems 
    End Function

Open in new window


If I check the FoundItems object when it actually makes it all the way to the end of the function, I see that it actually contains an item.  But, when I get back to the calling function, the List(Of ItemDetails) that I tried to return appears to be invalid.  Is there something that I'm missing/overlooking here?

P.S., I know I shouldn't have, but I mistakenly posted this question as a comment on another thread.  Any assistance you can provide would be greatly appreciated.
0
khufford19
Asked:
khufford19
  • 4
  • 2
2 Solutions
 
Paul JacksonCommented:
In the code below you add Item to the list but never actually assign anything to item.
        Try  
            For I As Long = 0 To DatabaseItems.Rows.Count - 1  
                Dim Item As New ItemDetails  
                Dim CurrentItem As DataRow  
  
                CurrentItem = DatabaseItems.Rows(I)  
  
                ' *********************************************************************  
                ' **Here's where the Item object is populated.  
                ' *********************************************************************  
  
                I = I + 1  
  
                ' *********************************************************************  
                ' ** Add the waiver we just populated to the array list, reset the   **  
                ' ** temporary waiver object and check for the next record.          **  
                ' *********************************************************************  
                FoundItems.Add(Item)  
                Item = Nothing  
                CurrentItem = Nothing  
            Next I  

Open in new window

0
 
khufford19Author Commented:
Yeah, there's a LOT of code that I "masked" with the line I put that says "Here's where the Item object is populated".  In there I do a number of data validation checks and other tests that get really mundane and repetetive.  An example of part of what would go in here would be:
                With Item
                    If Not IsDBNull(CurrentItem("Location")) And Len(Trim(CurrentItem("Location").ToString)) > 0 Then
                        .Location= Replace(UCase(Trim(CurrentItem("Location").ToString)), "'", "''")
                    End If

                    If Not IsDBNull(CurrentItem("Acquired")) Then
                        .Acquired= CurrentItem("Acquired")
                    End If

Open in new window

...and so forth and so on.  As I stated, when I look at the Item and the FoundItems List(Of ItemDetails) objects when it reaches the end of the function, they are both correctly populated.  It's only when it returns to the calling function that I seem to lose everything.  If I return a value of Nothing, the calling function correctly receives that value.  Otherwise, the List(Of ItemDetails) object can't be indexed for any of its properties.
0
 
srikanthreddyn143Commented:
I dont see any flaw in the code. Is the code throwing any exception or the list is just not being populated.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
khufford19Author Commented:
No, I'm not getting any exception thrown.  When it comes back to the calling function, if I mouse over the ExistingItems object, the Intellisense shows "ExistingWaivers: Count = Property evaluation failed."  If I drill down, it indicates that none of the properties for the object can be evaluated.
0
 
srikanthreddyn143Commented:
your variable is existingitems. Right? Where did this existingwaivers came from?
0
 
khufford19Author Commented:
Whoops.  Yeah, that was a typo.  I'm working on a couple of projects at the moment.  The variable name is ExistingItems.
0
 
khufford19Author Commented:
Okay, now I feel like a total moron.  I think I just found the source of my problem, and it turns out that the object is actually getting returned (although the IDE doesn't really give any indication that it is).

I did receive an exception further along in the process, which had me scrutinizing the object that was returned from my function.  I was so focused on the fact that it didn't look like I was getting an object back, that I completely forgot about the "index out of range" exception message that occurred during the For I = 0 to ExistingItems.Count loop.

So what was the problem?  I should've used For I = 0 to ExistingItems.Count - 1 instead.  It was trying to go to a non-existant item in the list.  I apologize for the confusion.  Thanks for your help.  I've split the points between each of you that commented.
0

Featured Post

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.

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