I am trying to code a Visual Basic 2019 console app that makes use of the StreamReader object to read text from a file. According to the Microsoft documentation for the StreamReader object, when it is initialized, you pass a string with the path of the file to be opened, such as
Using reader as StreamReader = new StreamReader(“c:\sample.txt”)
However, all of the Microsoft documentation has the path hard-coded, not as a variable. In my code, I am declaring a variable as type string, calculating its value at runtime, and passing that string to StreamReader as an argument.
Dim thisFile as string
Using reader as StreamReader = new StreamReader(thisFile)
This results in a compile-time error, BC30311 Value of type ‘String’ cannot be converted to ‘Stream’
What data type do I need to declare the variable as, instead of as a string, for this to compile and work?
You probably need to create a new STREAM object first (using the STRING pathname). I’m not VB oriented, but that would be my initial guess. What does Intellisense say about the StreamReader’s argument types?
--
This message has been scanned for viruses and dangerous content by
E.F.A. Project, and is believed to be clean.
Click here to report this message as spam.
--
--
You received this message because you are subscribed to the Google Groups "NLUG" group.
To post to this group, send email to nlug...@googlegroups.com
To unsubscribe from this group, send email to nlug-talk+...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/nlug-talk?hl=en
---
You received this message because you are subscribed to the Google Groups "NLUG" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nlug-talk+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nlug-talk/002f01d5a3b9%24257ec580%24707c5080%24%40jfeldredge.com.
Intellisense says to use a string argument. My guess is that I have run into a managed-code vs. unmanaged-code issue, and that a literal argument is being silently cast to the proper managed-code string type, but I haven’t found any documentation saying how to declare a managed-code string.
To view this discussion on the web visit https://groups.google.com/d/msgid/nlug-talk/016401d5a3b9%24c196a410%2444c3ec30%24%40jobsoft.com.
That is a bit odd, but then again, M$ VS. I wonder, though, unless it’s overloaded (STRING being one of them), it would sort of make sense to me as I normally thing of a STREAM being one of several source input types. Of course, VS has never been known to be buggy or lacking in Intellisense! :) The error sounds like when you have a type that can’t be automatically casted. I know in C# there is ‘string’ and there is ‘String’, and while they both more or less represent the same thing, they are distinctly different object types. Go figure!
To view this discussion on the web visit https://groups.google.com/d/msgid/nlug-talk/004201d5a3ba%247e52d560%247af88020%24%40jfeldredge.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nlug-talk/017d01d5a3bb%2491c200c0%24b5460240%24%40jobsoft.com.
OK, I found a work-around. I have to use an additional, undocumented step.
Using fs As FileStream = New FileStream(thisFile, FileMode.Open)
Using reader As StreamReader = New StreamReader(fs)
fileText = reader.ReadLine()
End Using
End Using
I found examples of code online that used my earlier approach, so I am guessing that the need to wrap the file into a FileStream is new, and Microsoft hasn’t bothered to update the documentation, since all of their examples are using hard-coded file paths, not variables.
The “Using” statements are a way to make Visual Basic handle the garbage collection once the objects are no longer in scope.
From: nlug...@googlegroups.com <nlug...@googlegroups.com> On Behalf Of jo...@jfeldredge.com
Sent: Monday, November 25, 2019 11:53 AM
To: nlug...@googlegroups.com
Subject: [nlug] [OT] Visual Basic Question
I am trying to code a Visual Basic 2019 console app that makes use of the StreamReader object to read text from a file. According to the Microsoft documentation for the StreamReader object, when it is initialized, you pass a string with the path of the file to be opened, such as
--
Good deal! I’ve found that devops forum sites like Stack Exchange, Stack Overflow, CodeWorks, Quora and Spiceworks always seem to do more for me than most “official” MSDN .Net/Class “documentation”! :) I sometimes think the tech writers for M$ are, for the most part, really only writing for each other! :)
From: nlug...@googlegroups.com <nlug...@googlegroups.com> On Behalf Of jo...@jfeldredge.com
Sent: Monday, November 25, 2019 1:20 PM
To: nlug...@googlegroups.com
To view this discussion on the web visit https://groups.google.com/d/msgid/nlug-talk/00a901d5a3c5%244855e050%24d901a0f0%24%40jfeldredge.com.