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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:55:37+00:00 2026-06-08T04:55:37+00:00

In my program, I am basically reading in a file, doing some processing to

  • 0

In my program, I am basically reading in a file, doing some processing to it, and then passing it back to the main program as a memorystream, which will be handled by a streamreader. This will all be handled by a class beside my main.

The problem is, when I return the memory stream from my method in another class, the “canread” variable is set to false, and thus causes the streamreader initialization to fail.

Below is an example of the problem happening (though in here I’m writing to the memorystream in the other class, but it still causes the same error when i pass it back.

In the class named “Otherclass”:

public static MemoryStream ImportantStreamManipulator()
{
   MemoryStream MemStream = new MemoryStream();

   StreamWriter writer = new StreamWriter(MemStream);
   using (writer)
   {
       //Code that writes stuff to the memorystream via streamwriter

       return MemStream;
   }
}

The function calls in the main program:

MemoryStream MStream = Otherclass.ImportantStreamManipulator();
StreamReader reader = new StreamReader(MStream);

When I put a breakpoint on the “return MemStream”, the “CanRead” property is still set to true. Once I step such that it gets back to my main function, and writes the returned value to MStream, the “CanRead” property is set to false. This then causes an exception in StreamReader saying that MStream could not be read (as the property indicated). The data is in the streams buffer as it should be, but I just can’t get it out.

How do I set it so that “CanRead” will report true once it is returned to my main? Or am I misunderstanding how MemoryStream works and how would I accomplish what I want to do?

  • 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-08T04:55:39+00:00Added an answer on June 8, 2026 at 4:55 am

    This is the problem:

    using (writer)
    {
        //Code that writes stuff to the memorystream via streamwriter
    
        return MemStream;
    }
    

    You’re closing the writer, which closes the MemoryStream. In this case you don’t want to do that… although you do need to flush the writer, and rewind the MemoryStream. Just change your code to:

    public static MemoryStream ImportantStreamManipulator()
    {
       // Probably add a comment here stating that the lack of using statements
       // is deliberate.
       MemoryStream stream = new MemoryStream();
    
       StreamWriter writer = new StreamWriter(stream);
       // Code that writes stuff to the memorystream via streamwriter
    
       writer.Flush();
       stream.Position = 0;
       return stream;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically I want to make simple toggle program (that will be mapped to some
So I'm reading in a file (like java program < trace.dat) which looks something
Basically, the program is asking for a user entered product name which will be
The program is basically about reading from a text file, storing the current data
I basically have a program that filters records from one excel file to another
My program basically looks like this: using(XmlReader reader = XmlReader.Create(File.Open(path, FileMode.Open, FileAccess.Reader, FileShare.Read)) {
Basically I have some sort of third party software running on Windows 7 which
I am creating a program that will download a .jar (java) file from a
I have written a program for reading the contents of a file. import java.io.*;
Below is the program I have for reading the .csv file and printing the

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.