Question Pass a Stream from a class

TBossAZ

Active member
Joined
Mar 31, 2009
Messages
43
Programming Experience
5-10
Apologies if i post this in the wrong area.

I have written a class that parses HTML files to retreive data. This is part of the code in that class:

VB.NET:
Private streamHTMLCode As Stream
Dim fileReader As New WebClient()
streamHTMLCode = fileReader.OpenRead(strPath)
Dim reader As StreamReader = New StreamReader(streamHTMLCode)
Dim strHTMLCode As String = ""

Do Until reader.EndOfStream
   strHTMLCode &= reader.ReadLine()
Loop
reader.Close()

what I want to do is take the value of streamHTMLCode and make it a property of a class like so:

VB.NET:
Public ReadOnly Property HTMLCode() As Stream
   Get
      Return streamHTMLCode
   End Get
End Property

The problem is when I try to access the property in my code (yes a HTML file was loaded first), I get an error that says: Stream was not readable

Is it not possible to pass Streams or to have streams as properties of a class?

Any help would be appreciated.
 
Is it not possible to pass Streams or to have streams as properties of a class?
A Stream reference can be passed on, after all that was what the WebClient did to you, right?
 
I undestand that the OpenRead() method of a WebClient returns the html code as a stream. That stream is stored in a stream variable. My question is how do I access that stream variable from the class? Like I said before, I get a Stream was not readable error message.
 
I don't know your code.
 
Sorry for the delay.

Here is the code where I try to access the streamHTMLCode via the HTMLCode property of the class (code for the class already provided above):

VB.NET:
'the path of the html to be parsed is passed in the constructor of the class
Dim oMyObject as New MyClass(htmlpath)
Dim streamCode As Stream = oMyObject.HTMLCode
oMyObject = Nothing

Dim reader As StreamReader = New StreamReader(streamCode)
Dim strHTMLCode As String = ""

Do Until reader.EndOfStream
   strHTMLCode = reader.ReadLine()
   'list box to display code line by line
   lstEventViewer.Items.Add(strHTMLCode)
Loop

This is the code where I get the Stream was not readable error message.

Let me know if you have any more questions.
 
So how about creating a function that accepts the html file as a parameter and returns a string which is the result of the stream that the function uses internally?

Or if this is a class then the html file path is a string already stored, just make a GetContents() function that returns a string which the the result of the stream.
 
Thanks for the reply JB, but I was hoping to find an explanation on why I cannot pass a Stream or get a Stream-based property from a class. Any ideas?
 
This is the code where I get the Stream was not readable error message.

Let me know if you have any more questions.
The code in your first post includes code that reads through and closes the stream. Is this in any way related to the stream you attempt to pass on out of the class?
 
The code in your first post includes code that reads through and closes the stream. Is this in any way related to the stream you attempt to pass on out of the class?

Yes it is. In reference to the first code I posted, streamHTMLCode is the Stream variable that holds the HTML Code as a stream from fileReader.OpenRead(). reader is a StreamReader that I use to read the stream, which I later close using reader.Close()

Do you think that closing the StreamReader is the cause of the problem? Would the stream itself still not be stored in streamHTMLCode?
 
You can only read a stream once if CanSeek return False, if you close a stream (or the StreamReader) then it can't be opened again. Web request response streams can normally only be read once, while local file system streams normally support seek. If you need a web request to remain available as a stream that the consumer can seek and read multiple times you have to first transfer it to a local cache and expose this, for example a MemoryStream.
 
Back
Top