Question Stringwriter this time

badbane

Member
Joined
Apr 23, 2014
Messages
8
Programming Experience
Beginner
maybe I am not understanding how string writer works because my declaration produces an error.

VB.NET:
Dim swtoner As New StringWriter(txtInvtoner)

This is my declaration above and below is the error when I click the button this code is attached to. txtInvtoner is my path info to the text file.

VB.NET:
Unable to cast object of type 'System.String' to type 'System.IFormatProvider.
 
The StringWriter class has four constructors: one with no parameters, one with just a StringBuilder parameter, one with just an IFormatProvider parameter and one with both StringBuilder and IFormatProvider parameters. You are trying to invoke a constructor with a single parameter so there are only two choices. What type is 'txtInvtoner'? If it's not type StringBuilder or type IFormatProvider then it's obviously not going to work because those are the only types that the StringBuilder constructor accepts. From the error message, 'txtInvtoner' is presumably type String, so that's not going to work.

A StringWriter can't actually write to a String object because Strings are immutable, i.e. cannot be changed once created. It can only write to a StringBuilder which is then used to build a String. Either you have to provide a StringBuilder or one will be created automatically. At any point, you can get a String containing the text written so far by calling ToString on the StringWriter or else calling GetStringBuilder to get the internal StringBuilder and then calling ToString on that.
 
using a stringwriter inside a loop

okay so here is what I am attempting to to. I am trying to take the variable in the textboxes move them into an array and they write each value in the array to a line in the text file.

VB.NET:
 Dim swtoner As New StringWriter()


        

        Toneinv(0) = txtDell330.Text
        Toneinv(1) = txtLex360.Text
        Toneinv(2) = txtLJ4000.Text
        Toneinv(3) = txtLJ4100.Text
        Toneinv(4) = txtLex260.Text
        Toneinv(5) = txtDell1815.Text
        '    Dim kj As New List(Of String)
        For i = 0 To UBound(Toneinv)
            swtoner.WriteLine(txtInvtoner)
        Next

        swtoner.Close()
txtinvtoner is the text file but I am not sure how to take the tonerinv(0-5) write to the text file on a new line.

So once it is done the text file should look like this

1
2
2
1
3
1

which is what was in the textboxes when i clicked on the button.
 
If you want to write text to a file then you would use a StreamWriter, not a StringWriter. A StreamWriter writes text to any type of Stream but the default is a FileStream. If the only purpose of the array is to hold the text before you write to the file then the array is pointless. Just write the text directly from the TextBoxes to the StreamWriter. If you need the array for some other purpose then populate it first and then just call File.WriteAllLines to write the whole array to a file in one go. It will create the StreamWriter internally so you don't have to worry about it.
 
If you want to write text to a file then you would use a StreamWriter, not a StringWriter. A StreamWriter writes text to any type of Stream but the default is a FileStream. If the only purpose of the array is to hold the text before you write to the file then the array is pointless. Just write the text directly from the TextBoxes to the StreamWriter. If you need the array for some other purpose then populate it first and then just call File.WriteAllLines to write the whole array to a file in one go. It will create the StreamWriter internally so you don't have to worry about it.

okay so I did what you said but there was a problem.
VB.NET:
Toneinv(0) = txtDell330.Text
        Toneinv(1) = txtLex360.Text
        Toneinv(2) = txtLJ4000.Text
        Toneinv(3) = txtLJ4100.Text
        Toneinv(4) = txtLex260.Text
        Toneinv(5) = txtDell1815.Text
        '    Dim kj As New List(Of String)
       
        File.WriteAllLines(file path,Toneinv(ubound(toneinv)))

here is the error i get with the file.writealllines

Unable to cast object of type 'System.String' to type 'System.Collections.Generic.IEnumerable`1[System.String]'.
 
Last edited:
You would proceed how you should have proceeded in the first place: pay attention to Intellisense as you type because it tells you exactly what you can pass to the method or else use the Help menu to open the documentation for the method and see what parameters it has. Of course, you could just stop and think about it. What is it that WriteAllLines does? It writes lines to a file, right? What would be the minimum of information that you would have to give it in order for it to do that? The lines to write and the file to write them to, maybe? Perhaps you should use Intellisense and/or the documentation to confirm that.
 
ah I kept thinking you had to define the length of the array for the writealllines but you don't. Thanks for the help.
this is the resolution in case another lost souls wanders this way.

VB.NET:
Toneinv(0) = txtDell330.Text
        Toneinv(1) = txtLex360.Text
        Toneinv(2) = txtLJ4000.Text
        Toneinv(3) = txtLJ4100.Text
        Toneinv(4) = txtLex260.Text
        Toneinv(5) = txtDell1815.Text
        '    Dim kj As New List(Of String)
       
        File.WriteAllLines(file path,Toneinv))
 
Back
Top