3

I have an object that has a byte array property 'ZipFile' to store a file stream:

Property in Result class:

public class Result
{
  public byte[] ZipFile;
}

In my application, I generate a PDF file, and read the file into the 'ZipFile' property using my ReadFile method like this :

objResult.ZipFile = ReadFile(FilePath);

Signature of ReadFile method:

private byte[] ReadFile(string strFileName)

The Problem:

My ReadFile method will now be called in a loop, because I am generating multiple PDF files. Each time the ReadFile method will read a new file from the specifed parameter to the 'objResult.ZipFile' property, consequently replacing the old value in 'ZipFile' property. Now, I want my 'ZipFile' property to store multiple PDF files stream. So what should I do for that? Should I just change this property to a two dimensional byte[][] array, or is there any better way to do this? Remember, that this property will be used for saving(writing) these files by calling method. Open to all Suggestions. Thanks.

4 Answers 4

4

Sounds like you should either have a List<Result>, or result should have a collection such as List<byte[]> as a ZipFiles property. Note that currently you don't have a property at all - you have a public field, which is generally a bad idea.

(You probably wouldn't expose it as a List<byte[]> - that would be the underlying implementation. I'd probably make it an IEnumerable<byte[]> and expose an AddZipFile method.)

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your answer. I will be returning a Result object to the calling method, to Read the stream from its property. So, Why should I expose an AddZipFile method?
@ebad86: Well something has to add the byte array to the list - but it's generally a bad idea to expose whole lists as properties.
Thanks for the information. I am committing this mistake a lot. Although, I am desparate to learn why is it a bad idea. Is this topic mentioned in any of your articles or book? Can you please guide where I can learn more about this?
Yes, there are some guidelines around this in appendix B of C# in Depth, IIRC.
2

You may use List<byte[]> (list of byte array) and then append the array in the list in each iteration. Something like.

public class Result
{
   public List<byte[]> ZipFilesList { get; set; };
}

Later you can do:

ResultObj.ZipFilesList.Add(ReadFile(FilePath));

1 Comment

Thanks. I will do this and let you know.
1

If you just want to store them for later processing a list or queue will do it. I think I would opt for a queue as your usage pattern seems to match that.

 // make queue
 var filesQueue = new Queue<byte[]>();

 // add file
 filesQueue.Enqueue(newFile);

 // get file
 var fileToSave=filesQueue.Dequeue();

You can switch to a concurrentQueue later if you would ever need to parallelize etc..

Comments

1

You need to have list of List<byte[]>

public class Result
{
   public List<byte[]> ZipFiles;
}

And add the files into your list

objResult.ZipFiles.Add(ReadFile(FilePath));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.