Solved

Problem returning generic List(Of T) object

Posted on 2011-02-28
7
277 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
ID: 34999163
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
ID: 34999308
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
ID: 34999310
I dont see any flaw in the code. Is the code throwing any exception or the list is just not being populated.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:khufford19
ID: 34999518
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
ID: 34999578
your variable is existingitems. Right? Where did this existingwaivers came from?
0
 

Author Comment

by:khufford19
ID: 34999923
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
ID: 35000412
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

895 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

12 Experts available now in Live!

Get 1:1 Help Now