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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:11:00+00:00 2026-06-01T17:11:00+00:00

Alright, I have a class EIR which is a List based from its base

  • 0

Alright, I have a class “EIR” which is a List based from its base class “ExpenseReport” as such:

    using System;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.IO;
using System.Linq;
using System.Text;

namespace Payroll
{
    [Serializable]
    public class EIR : List<ExpenseItem>
    {   

    public void WriteToFile(string filename)
        {
            try
            {
                BinaryFormatter formatter = new BinaryFormatter();
                FileStream stream = new FileStream("D:\\myExpensesUpdated.bin", FileMode.Create, FileAccess.Write, FileShare.None);
                formatter.Serialize(stream, filename);
                stream.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Unable to serialize Expenses: {0}", ex.Message);
            }
        }

        public static EIR ReadFromFile(string filename)
    {
        EIR iRost = new EIR();
        try
        {
            BinaryFormatter formatter = new BinaryFormatter();
            FileStream stream = new FileStream("D:\\myExpensesUpdated.bin", FileMode.Open, FileAccess.Read, FileShare.Read);
            iRost = (EIR)formatter.Deserialize(stream);

        }
        catch (Exception ex)
        {
            Console.WriteLine("Unable to deserialize Expenses: {0}", ex.Message);
        }

        return iRost;            
    }            
        }
    }
}

This ReadFromFile part is where I’m having a problem:

In the main program, this portion tries to call ReadFromFile and display newly updated items but I get the error “Unable to cast object of type ‘System.String’ to type ‘Payroll.EIR'”.

try
        {
            EIR expensesUpdated = EIR.ReadFromFile("D:\\myExpensesUpdated.bin");
            foreach (var e in expensesUpdated) Console.WriteLine("Updated: {0}", e.ToString());
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

How do I get the iRost to cast to the Payroll.EIR which is a class? Is there some way to convert a serialized string to a class?

  • 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-01T17:11:01+00:00Added an answer on June 1, 2026 at 5:11 pm

    Your problem is already in WriteToFile function where you are actually serializing the filename instead of the class. I didn’t try it but this should work:

    public void WriteToFile(string filename)
    {
        try
        {
            BinaryFormatter formatter = new BinaryFormatter();
            FileStream stream = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None); // filename parameter instead of fixed path
            formatter.Serialize(stream, this); // class instance instead of filename parameter
            stream.Close();
        }
        catch (Exception ex)
        {
            Console.WriteLine("Unable to serialize Expenses: {0}", ex.Message);
        }
    }
    
    public static EIR ReadFromFile(string filename)
    {
        EIR iRost = new EIR();
        try
        {
            BinaryFormatter formatter = new BinaryFormatter();
            FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read); // filename parameter instead of fixed path
            iRost = (EIR)formatter.Deserialize(stream);
        }
        catch (Exception ex)
        {
            Console.WriteLine("Unable to deserialize Expenses: {0}", ex.Message);
        }
    
        return iRost;            
    }            
    

    Are you sure it’s a good idea to silently catch all exceptions and just write a message to console?

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

Sidebar

Related Questions

Alright, so I have a base class which we'll call TFruit . From this
Alright lets say I have a cPlayer class that inherits from cOrganism , which
Alright so here is the question. I have a user class which contains a
Alright I have a gigantic list of files I would like to copy from
Alright, I have jQUery function which adds class to a div#float once div#floatLunch gets
Alright, I have a UIView which displays a CGPath which is rather wide. It
I am using STI for my user models. I have an User class, and
Alright, I have a fairly simple design. class Update(models.Model): pub_date = models.DateField() title =
Alright, so I have a button that when clicked, will change the class of
I have a pretty simple HABTM set of models class Tag < ActiveRecord::Base has_and_belongs_to_many

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.