Hi folks!
Well, take a look:
int nfiles = arrArchives.Count;
Stream st = null;
for (int i = 0; i <= nfiles; i++)
{
st = arrArchives[i];
}
So, I want add arraylist items into "st" stream, how to do this?
This below code don´t works, the error: "Cannot convert source type 'object' to target type 'System.IO.Stream'"
Thanks in advanced :)
How to add arraylist items to a unique stream
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Former memberPosted Apr 24, 2008, 3:07 PM EDT
int nfiles = arrArchives.Count;
Stream st = null;
for (int i = 0; i <= nfiles; i++)
{
st = (Stream)arrArchives[i];
}
Althought its seems kind of redundant, lol.
However, if the objects you are storing in the arraylist arent of type Stream or derive from Stream, you will get an InvalidCastException.
If your just trying to write to the stream, you will need to initalize the stream before you can write to it. If you just want to store the data, you can use a MemoryStream, something like this:
int nfiles = arrArchives.Count;
Stream st = new MemoryStream();
for (int i = 0; i <= nfiles; i++)
{
//This will cause a compile error though...
st.Write(arrArchives[i]);
}
But we need to know what your storing in the ArrayList collection before we can get any further :)
Former memberPosted Apr 24, 2008, 3:14 PM EDT
List<Stream> arrArchives = new List<Stream>();
This will create a generic collection much like the ArrayList, except no casting is involved. So instead of storing objects in the collection, your storing Stream objects, which makes it much more typesafe because when you use the collection, you will already know whats stored in it and wont have to backtrack or remember.