tags:

views:

62

answers:

3

I have this code to return a list of fund sources for our organization.

    Dim FundSourceList As New List(Of FundSource)

    Dim fs As New FundSource

    If results.Count > 0 Then
        For Each result In results


            fs.FundID = result.Item("strFundID")
            fs.FundDescription = result.Item("txtFundIDDescr")
            fs.ShortFundDescription = result.Item("txtFundIDDescrShort")

            FundSourceList.Add(fs)

        Next
    End If


    Return FundSourceList

The problem is that when I loop through the resulting FundSourceList all it shows is the last value. For example, if I have three fund sources (state, federal, athletic), then when I use this code to loop through all I get listed is athletic, athletic, athletic.

    For Each FundSource In FundSources
        Debug.Print(FundSource.FundDescription)
    Next

So I change the code to this. I moved the creation of the fs variable inside the loop.

    Dim results = From result In dsResult.Tables(0) Select result

    Dim FundSourceList As New List(Of FundSource)



    If results.Count > 0 Then
        For Each result In results
            Dim fs As New FundSource

            fs.FundID = result.Item("strFundID")
            fs.FundDescription = result.Item("txtFundIDDescr")
            fs.ShortFundDescription = result.Item("txtFundIDDescrShort")

            FundSourceList.Add(fs)

        Next
    End If


    Return FundSourceList

This works fine but now I'm creating a new class over and over again. It seems a little inefficient to me. Can I not create the class outside the loop and use it over and over again? Thanks.

+1  A: 

If you have 3 fund sources, you need three FundSource objects. It's as simple as that. I don't know what's inefficient about it...

How can you add 3 fund sources to your list but just create one?

Meta-Knight
A: 

You're not actually creating a class - the class is the code definition for the methods and properties. When you use the New operation, you're creating an instance of that class, which results in an object. When you have a list of objects, like FundSourceList, you want the items in it to be individual objects. So yes, the solution you have at the bottom is correct. You mention efficiency concerns - when you instantiate the object, basically all that is happening (in this case) is some memory is being allocated to store the variables (and some references for the managed memory, but you don't need to worry about that here). This is necessary and is optimized under-the-hood, so you shouldn't need to worry about that either.

James Kolpack
A: 

You can't instantiate the object outside of the loop to achieve the result you're after.

This is because your object would be a reference type.

By instantiating outside of the loop, you would create one reference to your object.

When iterating through your results and setting the properties, you'll be using that same reference over and over.

All you're adding to the list on each iteration is the same reference, which by the end of the loop, will refer to an object containing the last values in your result set.

By creating new objects inside the loop, you create new references - each pointing to a new FundSource. Your loop now writes into a fresh object, and get your desired results.

Paul Alan Taylor