0

I need a solution to my current code, I am trying to save text to a file from a Text Box, and add a string to it. My following code is:

 Dim fs As FileStream = File.Create(fileName.Text)

    ' Add text to the file. 

    Dim info As Byte() = New UTF8Encoding(True).GetBytes(CodeBox.Text)

    Dim Code = "-- Made with LUA Creator by Sam v1.9
    " + info
    fs.Write(Code, 0, Code.Length)
    fs.Close()

    MsgBox("File saved as " + fileName.Text)

But Visual Studio says that I cannot use "+" operator with strings & bytes:

Error BC30452 Operator '+' is not defined for types 'String' and 'Byte()'.

Anyone have a solution? Sorry if this is a duplicate, I couldn't find it anywhere here so I just asked myself. Thanks.

1
  • 3
    & is for string concatenation; + is for addition. Try concatenating your string with the TextBox, before usingGetBytes: "-- Made with..." & CodeBox.Text) Commented May 31, 2015 at 23:08

1 Answer 1

1

"Can I Convert A Byte() to a string?" Short answer is yes, but that doesn't look like what you're really wanting to do.

You're trying to concatenate a String with a Byte array, which Dim Code has no idea what the end result is supposed to be.

FileStream.Write() requires a Byte array so you can try a couple of things

  1. Concatenate the string from the TextBox with your "header" information then turn it into a Byte array.

    Dim fs As FileStream = File.Create(fileName.Text)
    
    ' Add text to the file. 
    Dim Code As Byte() = New UTF8Encoding(true).GetBytes("-- Made with LUA Creator by Sam v1.9 " & CodeBox.Text)
    fs.Write(Code, 0, Code.Length)
    fs.Close()
    
  2. Write your "header" information, then write the Textbox information

    Dim fs As FileStream = File.Create(fileName.Text)
    
    ' Add text to the file. 
    Dim header As Byte() = New UTF8Encoding(true).GetBytes("-- Made with LUA Creator by Sam v1.9 ")
    Dim info As Byte() = New UTF8Encoding(True).GetBytes(CodeBox.Text)
    fs.Write(header, 0, header.Length)
    fs.Write(info, 0, info.Length)
    fs.Close()
    
Sign up to request clarification or add additional context in comments.

2 Comments

I got it to work, I used #1 only and it worked, thanks.
@samiles1995 You're welcome... Kindly give my answer a check mark so your question is solved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.