Kenzo
Kenzo

Reputation: 1837

Streams Concatenation error

I have two streams that i want to combine into one stream. The first stream is user-defined stringStream class that inherits from System.IO.Stream class and the second stream is a networkStream , I created an array of Stream with stores those two streams and pass it to a user-defined class combinedStream constructor as parameter. Below are the lines of code

StringStream stringStream = new StringStream("test string");
Stream[] streamArray = new Stream[2];
streamArray[0] = stringStream;
streamArray[1] = nsStream;//NetworkStream instance parameter
CombinedStream combinedStream = new CombinedStream(streamArray);
this.nsStream = (NetworkStream)combinedStream;

This the error i got : "Cannot convert CombinedStream to NetworkStream" What could be the reason?

Upvotes: 0

Views: 165

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1501163

Fairly simply, because CombinedStream (which is presumably your own class) is not a NetworkStream. I wouldn't expect it to be.

The big question is why nsStream is meant to be a NetworkStream anyway. Can you change the type so it's just Stream instead? That should be fine unless you're using any NetworkStream-specific methods... and if you're doing that, then what would you expect to happen when those methods were called on a CombinedStream?

Upvotes: 3

Adam V
Adam V

Reputation: 6356

Sounds like CombinedStream is not defined as a subclass of NetworkStream. Thus the last line: this.nsStream = (NetworkStream)combinedStream; will fail as an invalid conversion.

Upvotes: 1

Related Questions