Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 309569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:45:13+00:00 2026-05-12T07:45:13+00:00

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

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T07:45:14+00:00Added an answer on May 12, 2026 at 7:45 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have this code for populating a list of comments to return: foreach
I have this code: List<string> lineList = new List<string>(); in j = 0; Dictionary<string,
I have this code: public static List<ReplicableObject> ParseStreamForObjects(Stream stream) { List<ReplicableObject> result = new
I have this following code which will return all the current semesters. How do
i have this code $(function() { $('#ans_vote a span').click(function(){alert('working');return false;});}); and this html <div
Right now, I have this code: bool isAnyTrue() { for(std::list< boost::shared_ptr<Foo> >::iterator i =
I have this code: public static String SelectRandomFromTemplate(String template,int count) { String[] split =
I have this Code for Reading Excell Records: public IEnumerable<FillinEntity> Map(IEnumerable<ExcelRow> excelRows) { List<FillinEntity>
I need your opinion about this code below. I have a list of messages:
i have this restful web service where i actually return a list<> in c#.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.