'1-dimensional array of String' cannot be converted to '1-dimensional array of Byte' because 'String'

I'm stuck. I am trying to take the string values of 4 form fields and write it to a binary file. Below is my code which errors on the last line per the subject of this post.

Please help, and Thank you in advance.

' Create Binary file
Dim tmpSel, tmpExcl, tmpExch, tmpSysS As String
Dim arrayVal(3) As String

If Me.txtSel.Text = Nothing Then
MsgBox("Selections can't be blank!" & vbCrLf & vbCrLf & _
"Save Failed.", MsgBoxStyle.OkOnly)
GOTO Failed
Else
tmpSel = Me.txtSel.Text
End If
arrayVal(0) = tmpSel

If Me.txtExcl.Text = Nothing Then
tmpExcl = Nothing
Else
tmpExcl = Me.txtExcl.Text
End If
arrayVal(1) = tmpExcl

If Me.chkExch.CheckState = CheckState.Checked Then
tmpExch = "ExchVal"
Else
tmpExch = Nothing
End If
arrayVal(2) = tmpExch

If Me.chkSyS.CheckState = CheckState.Checked Then
tmpSysS = "SysS"
Else
tmpSysS = Nothing
End If
arrayVal(3) = tmpSysS

My.Computer.FileSystem.WriteAllBytes _
("C:\MyDocuments\CollectionData.dat", arrayVal, True)



Answer this question

'1-dimensional array of String' cannot be converted to '1-dimensional array of Byte' because 'String'

  • llebron

    That fixed it.

    Thanks for the help.


  • gmork

    There is no Dim keyword inside the For Each loop

    If you like to inialize a the loop variable inside the For loop it will look like:

    For Each element As String In SomeCollecion

    ....

    Next



  • John J. London

    Yes I want to write each string to it's on line, but the file has to be a binary file.
  • Michael Zill

    This will convert a string to a Byte array which you can then use with WriteAllBytes

     

    Dim OutputString As String = ""

    For Each Dim s As String In arrayVal
        OutputString = OutputString & s & vbCrLf
    Next

    Dim b() As Byte = ConvertStringToByteArray(OutputString)
    My.Computer.FileSystem.WriteAllBytes("C:\MyDocuments\CollectionData.dat", b, True)

     

    Public Shared Function ConvertStringToByteArray(ByVal StringToConvert) As Byte()
        Return (New System.Text.UnicodeEncoding).GetBytes(StringToConvert)
    End Function


  • Interesting

    Dim OutputString As String = ""
    For Each Dim s as string In arrayVal
    OutputString = OutputString & s & vbCrLf
    Next

  • Aaron Hare - MSFT

    I'm not sure what you want to write to the file but looking at you code you look to be creating and array of string and want this written to a file.

    The following will do this....

    Dim OutputString As String = ""
    For Each Dim s As String In arrayVal
    OutputString = OutputString & s & vbCrLf
    Next

    My.Computer.FileSystem.WriteAllText("C:\MyDocuments\CollectionData.dat", OutputString, True)

    WriteAllBytes is probably more for writing binary data - say a graphics image where you provide a byte array.


  • Jim Selinsky

    my mistake....


  • MichaelLaw

    So the final code should be

    spotty wrote:

    Dim OutputString As String = ""

    For Each s As String In arrayVal
    OutputString = OutputString & s & vbCrLf
    Next

    Dim b() As Byte = ConvertStringToByteArray(OutputString)
    My.Computer.FileSystem.WriteAllBytes("C:\MyDocuments\CollectionData.dat", b, True)

    Public Shared Function ConvertStringToByteArray(ByVal StringToConvert) As Byte()
    Return (New System.Text.UnicodeEncoding).GetBytes(StringToConvert)
    End Function


  • sertdemiry

    Man I feel stupid. I tweaked the code and now I'm getting "Expression expected" for: Dim in the; "For each Dim s As String In arrayVal" line.

    Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click

    ' Create Binary file
    Dim tmpSel, tmpExcl, tmpExch, tmpSysS, s As String
    Dim arrayVal(3) As String

    If Me.txtSelections.Text = Nothing Then
    MsgBox("Selections can't be blank!" & vbCrLf & vbCrLf & _
    "Save Failed.", MsgBoxStyle.OkOnly)
    GoTo Failed
    Else
    tmpSel = Me.txtSelections.Text
    End If
    arrayVal(0) = tmpSel
    If Me.txtExclude.Text = Nothing Then
    tmpExcl = Nothing
    Else
    tmpExcl = Me.txtExclude.Text
    End If
    arrayVal(1) = tmpExcl
    If Me.chkExch.CheckState = CheckState.Checked Then
    tmpExch = vbCrLf & ExchVal
    Else
    tmpExch = Nothing
    End If
    arrayVal(2) = tmpExch
    If Me.chkSysState.CheckState = CheckState.Checked Then
    tmpSysS = vbCrLf & "SystemState"
    Else
    tmpSysS = Nothing
    End If
    arrayVal(3) = tmpSysS


    Dim OutputString As String = ""

    For Each Dim s In arrayVal
    OutputString = OutputString & s & vbCrLf
    Next

    Dim b() As Byte = ConvertStringToByteArray(OutputString)
    My.Computer.FileSystem.WriteAllBytes("C:\MyDocuments\CollectionData.dat", b, True)


    Failed:

    End Sub


    Public Shared Function ConvertStringToByteArray(ByVal OutputString As String) As Byte()
    Return (New System.Text.UnicodeEncoding).GetBytes(OutputString)
    End Function
    End Class


  • '1-dimensional array of String' cannot be converted to '1-dimensional array of Byte' because 'String'