Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding items to list results in duplicates. What is a better way?
    primarykey
    data
    text
    <p>I have this code to return a list of fund sources for our organization.</p> <pre><code> Dim FundSourceList As New List(Of FundSource) Dim fs As New FundSource If results.Count &gt; 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 </code></pre> <p>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.</p> <pre><code> For Each FundSource In FundSources Debug.Print(FundSource.FundDescription) Next </code></pre> <p>So I change the code to this. I moved the creation of the fs variable inside the loop.</p> <pre><code> Dim results = From result In dsResult.Tables(0) Select result Dim FundSourceList As New List(Of FundSource) If results.Count &gt; 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 </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload