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 3847326
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T16:32:05+00:00 2026-05-19T16:32:05+00:00

I have a class which is creating a list of objects called manufacturers. I

  • 0

I have a class which is creating a list of objects called manufacturers. I use an XML file to create the objects which are stored in an arrayed called ManufacturerList.

Below is code from the ManufacturerImport class. This is where I create the objects.

 private List<Manufacturer> ManufacturerList = new List<Manufacturer>();

    public void AddManufacturer(Manufacturer manu)
    {
        ManufacturerList.Add(manu);
    }

    public List<Manufacturer> GetManufacturers()
    {
        return ManufacturerList;
    }

As you can see, I need to pass this list to other parts of my code so I have a GetManufacturers function.

In my Main function I am using the following code:

 List<Manufacturer> mList = ManuImport.GetManufacturers();

         TextWriter tw = new StreamWriter(@"C:\manu.txt");

        foreach (Manufacturer manu in mList)
        {

           //Output name to txt file. 
            tw.WriteLine(manu.ManufacturerName);

            Console.WriteLine(manu.ManufacturerName);
            Console.WriteLine(manu.ShortManufacturerName);
            Console.WriteLine(manu.ManufacturerDirectory);
            Console.WriteLine(manu.ManuId);
            Console.WriteLine("------------------------");
        }



  //Forgot to include this in example. Has been in code from beginning. sorry for confusion
            tw.Close();

I have debugged the code and found that the list is successfully being copied over to mList. I am a little confused as to how the mList list can contain all the objects I require but when I step through the list, I dont get the correct output.

My list has 486 objects but the output only writes 333 of those to the txt file. What is also odd is that the console outputs different manufacturers to the list.

Any thoughts would be great.

Cheers

  • 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-19T16:32:05+00:00Added an answer on May 19, 2026 at 4:32 pm

    You need to enclose your TextWriter object in a using block.

    List<Manufacturer> mList = ManuImport.GetManufacturers();
    
    using(TextWriter tw = new StreamWriter(@"C:\manu.txt"))
    {
        foreach (Manufacturer manu in mList)
        {
    
           //Output name to txt file. 
            tw.WriteLine(manu.ManufacturerName);
    
            Console.WriteLine(manu.ManufacturerName);
            Console.WriteLine(manu.ShortManufacturerName);
            Console.WriteLine(manu.ManufacturerDirectory);
            Console.WriteLine(manu.ManuId);
            Console.WriteLine("------------------------");
        }
    }
    

    There are two reasons for this:

    First, fundamentally, TextWriter implements IDisposable, which means that you should always call Dispose() when you’re finished with the object. The using block in C# and VB.NET are language-specific mechanisms for ensuring that Dispose gets called. This is true for all objects that implement IDisposable, so it’s probably a good idea to look at other areas of your code and other framework objects that you’re using to ensure that you’re following this pattern. Some things you might want to pay particular attention to are:

    • Database connections
    • Anything IO related (files, streams of any kind, etc.)

    Second, for this particular case, the TextWriter class buffers the data that gets written (so, for example, if you write the data a character at a time, you don’t have disk IO for EVERY character, since it does it in “chunks”). As a result, the buffer has to be flushed in order for data to get written to the disk. This happens under three cases:

    • The buffer fills up under the course of using the object in code
    • You close the TextWriter either by calling Close() or Dispose()
    • You manually flush the buffer by calling Flush()

    The first one you have no control over, as it’s automatic. The latter should be an exceptional case, where you want all buffered data written to disk immediately but still want to keep the writer open. The second case–the most important–is what you’re missing.

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

Sidebar

Related Questions

I have tried to serialize the class which contains the list of objects but
I have an interface, IMessage and a class which have several methods for creating
I'm creating a class that will have one public method, which returns a value
I am creating application in MVC3. I have a ConferenceController which has a Create()
I have an action called Sessies. In this action i am creating 'Sessies' objects
I have a class called Question. I have another class called Questions which is
I have class which have one public method Start , one private method and
I have class LegacyClass which inherits OldBaseClass. I'm considering a change to introduce a
I have class Money which is an @Embeddable @Embeddable public class Money implements Serializable,
I have class A which extends the Activity class. This class is in package

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.