Data not valid

tsp1lrk

Member
Joined
Oct 18, 2007
Messages
7
Programming Experience
5-10
Hello--
the following code is giving me:
Operator '=' is not defined for types 'integer' and 'char' ---

what should it be?


Private Shared Function NextLine(ByVal stream As StreamReader) As String
Dim stemp As Integer = stream.Read()
Dim sReturn As String = ""

While stemp <> -1 AndAlso stemp <> Chr(10)
'W3C SPECIFIC

If stemp = "["c Then
stemp = """"c
End If
If stemp = "]"c Then
stemp = """"c
End If

sReturn += CChar(stemp)
stemp = stream.Read()
End While
Return sReturn
End Function
 
Er..

StreamReader.Read() returns you an int

You are trying to compare it to a char. The comparison is not valid. Either convert the read int32 to a char (Convert.ToChar) or convert the fixed char to an int (Convert.ToInt32)


Incidentally, this looks like a very lame, slow way of reading a file. Maybe youre not aware of how string allocations work but reading a 1 megabyte file in character by character and appending each one to a string, will result in a million string allocations and around 500,000,500,000 bytes of memory copy operations.. Yup.. to read your 1 megabyte file using the method you wrote here will make the CPU shift over 500 gigs of data.. Please.. for the love of efficiency and good coding, change this code!
 
Are you performing a replace on the whole file?

Dim sb as New StringBuilder(File.ReadAllText(YOUR_PATH))
sb.Replace("[", """").Replace("]", """")
Return sb.ToString()
 
Back
Top