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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:26:26+00:00 2026-06-10T13:26:26+00:00

In my class DiaryManager I got two List with two different types (T) and

  • 0

In my class “DiaryManager” I got two List with two different types (T) and I want to save it to file, then I want to load it.
I got it to work with one of my list’s as I am going to show you.

My list that I save and load in my working code is named “m_diary”. The save method is this one:

    /// <summary>
    /// Saves the object information
    /// </summary>
    public void Save()
    {
        // Gain code access to the file that we are going
        // to write to
        try
        {
            // Create a FileStream that will write data to file.
            FileStream writerFileStream =
                new FileStream(DATA_FILENAME, FileMode.Create, FileAccess.Write);
            // Save our dictionary of friends to file
            m_formatter.Serialize(writerFileStream, m_diary);

            // Close the writerFileStream when we are done.
            writerFileStream.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        } 
    }

And my load method is this one:

    /// <summary>
    /// Load the object to the program 
    /// </summary>
    public void Load()
    {

        // Check if we had previously Save information of our friends
        // previously
        if (File.Exists(DATA_FILENAME))
        {

            try
            {
                // Create a FileStream will gain read access to the
                // data file.
                FileStream readerFileStream = new FileStream(DATA_FILENAME,
                    FileMode.Open, FileAccess.Read);
                // Reconstruct information of our friends from file.
                m_diary = (List<Diary>)                    
                    m_formatter.Deserialize(readerFileStream);
                // Close the readerFileStream when we are done
                readerFileStream.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            } 

        } 

    }

The “DATA_FILENAME” is this constant:

    private const string DATA_FILENAME = "TrainingDiary.dat";

This code works perfect from my windows form class.
But now Iv added one more list with a different type.

How do I save and load that second list to?? 🙂

Best regards
Cyrix

  • 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-06-10T13:26:27+00:00Added an answer on June 10, 2026 at 1:26 pm

    You can do it using similar code for the second list, or you can write a generic method:

       public static void Save<T>(string fileName, List<T> list)
        {
            // Gain code access to the file that we are going
            // to write to
            try
            {
                // Create a FileStream that will write data to file.
                using (var stream = new FileStream(fileName, FileMode.Create, FileAccess.Write))
                {
                    var formatter = new BinaryFormatter();
                     formatter.Serialize(stream, list);
                }
    
            }
            catch (Exception ex)
            {
               Console.WriteLine(ex.Message);
            }
        }
    

    And a load method:

       public static List<T> Load<T>(string fileName)
        {
            var list = new List<T>();
            // Check if we had previously Save information of our friends
            // previously
            if (File.Exists(fileName))
            {
    
                try
                {
                    // Create a FileStream will gain read access to the
                    // data file.
                    using (var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                    {
                        var formatter = new BinaryFormatter();
                        list = (List<T>)
                            formatter.Deserialize(stream);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
    
            }
            return list;
        }
    

    Usage of Load:

            var list = new List<string> {"one", "two", "three"};
            Save("first.dat", list);
    
            var list2 = Load<string>("first.dat");
            foreach (var VARIABLE in list2)
            {
                Console.WriteLine(VARIABLE);
            }
    

    Also see using Statement to handle open/close streams;

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

Sidebar

Related Questions

Class A { string name; IList<A> minorList = new List<A>(); } IList<A> majorList =
class mydialog declaration. ........................... my work is to create a window with a button
class Widget; std::vector< std::shared_ptr<Widget> > container class Criterium { public: bool operator()(const Widget& left,
Class 1 private void checkDuplicateCustomer(BulkCustomerVO bulkCustomerVO) { PagedDuplicateCustomerVO duplicateCustomerVO = new PagedDuplicateCustomerVO(); duplicateCustomerVO.setCustomer(bulkCustomerVO.getCustomerVO()); duplicateCustomerVO
class StartAction(argparse.Action): def __call__(self, parser, namespace, values, option_string=None): print "Hello" start.add_argument('-s', '--start', action=StartAction) I
class A: pass def b(self): print('b') A.b = b a = A() At this
class X { int i; public: X() { i = 0; } void set(int
class A attr_accessor :dab .... end Now I have an array of instances of
class A{ virtual int foo1(int a){ return foo1_1(a,filler(a)); } template<typename FunctionPtr_filler> int foo1_1(int a,
class Test { public static void main(String[] args) throws Exception { Test t =

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.