Solved

Problem returning generic List(Of T) object

Posted on 2011-02-28
7
273 Views
Last Modified: 2012-05-11
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
Comment
Question by:khufford19
  • 4
  • 2
7 Comments
 
LVL 29

Accepted Solution

by:
Paul Jackson earned 250 total points
Comment Utility
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
 

Author Comment

by:khufford19
Comment Utility
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
 
LVL 11

Assisted Solution

by:srikanthreddyn143
srikanthreddyn143 earned 250 total points
Comment Utility
I dont see any flaw in the code. Is the code throwing any exception or the list is just not being populated.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:khufford19
Comment Utility
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
 
LVL 11

Expert Comment

by:srikanthreddyn143
Comment Utility
your variable is existingitems. Right? Where did this existingwaivers came from?
0
 

Author Comment

by:khufford19
Comment Utility
Whoops.  Yeah, that was a typo.  I'm working on a couple of projects at the moment.  The variable name is ExistingItems.
0
 

Author Comment

by:khufford19
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now