vb.net String 类型的值无法转换为 Byte 的一维数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15330018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Value of type String cannot be converted to 1 dimensional array of Byte
提问by user2155147
Here is the declaration:
这是声明:
Public Class Client
Public Property Address() As String
Get
Return mAddress
End Get
Set(ByVal value As String)
mAddress = value
End Set
End Property
Public Property City() As String
Get
Return mCity
End Get
Set(ByVal value As String)
mCity = value
End Set
End Property
end sub
And the ERROR lies in here:
错误就在这里:
Public Function InsertClientRecordToDb(ByVal cli As Client) As Boolean
Dim retVal As Boolean
Dim dataSet As DataSet = New DataSet("dataSet")
dataSet.EnforceConstraints = False
'create table adapter object
Dim ClientTblAdapter As New CaseStudyDBDataSetTableAdapters.Client_TableTableAdapter
'check db connection
If ClientTblAdapter.Connection.State = ConnectionState.Closed Then
ClientTblAdapter.Connection.Open()
End If
'perform(insert)
If ClientTblAdapter.InsertClientRecord(cli.Clientcode, cli.Clientname, cli.Address, cli.City, cli.Contactperson, cli.Contactnumber) > 0 Then
retVal = True
End If**
Return retVal
End Function
The "cli.Address" and "cli.City" was underlined saying:
"cli.Address" 和 "cli.City" 下划线表示:
Value of type String cannot be converted to 1 dimensional array of Byte
String 类型的值无法转换为 Byte 的一维数组
What seems to be the problem?
似乎是什么问题?
回答by Parimal Raj
For the error it seems like you are trying to assign string to Byte()
对于错误,您似乎正在尝试将字符串分配给 Byte()
Something like :
就像是 :
Dim bArr As Byte() = "hello world!"
which is wrong, to convert string to Byte()you need to use the Encodingclass
这是错误的,要将字符串转换为Byte()您需要使用Encoding该类
Dim bArr As Byte() = System.Text.Encoding.Default.GetBytes("hello world!")

