我正在尝试从查询中获取三个字符串值,但这次调用函数:
argVals = GetArgValsForCompanyName(coName)
...无法使用错误信息编译,“类型'String'的值无法转换为'1维数组字符串'。”
该函数末尾的这一行也不编译:
Return args
...失败了,“类型'1维数组String'的值无法转换为'String'。”
我正在声明像这样的argVals,作为一个由三个val组成的字符串数组:
Dim argVals(2) As String
......和这样的功能:
Protected Function GetArgValsForCompanyName(coName As String) As String
有关更多上下文,以下是其(我认为)整体中的相关代码:
Private Sub Button1_Click( sender As Object, e As EventArgs) Handles Button1.Click
Dim connStr As String = "SERVER=PLATYPUS42;DATABASE=duckbilldata;UID=vonnegut;PWD=ryecatcher"
Dim upd8DML As String = "UPDATE CustomerCategoryLog SET Category = 'Exploding' WHERE Unit = @Unit And MemberNo = @MemberNo AND Custno = @CustNo"
Dim coName As String
Dim argVals(2) As String
Dim _Unit As String
Dim _MemberNo As String
Dim _CustNo As String
Dim curIndexVal As String
For Each cntrl As Control In Me.Controls
If TypeOf cntrl Is CheckBox Then
If DirectCast(cntrl, CheckBox).Checked = True Then
curIndexVal = CStr(DirectCast(cntrl, CheckBox).Tag)
coName = GetLabelTextForID(curIndexVal)
argVals = GetArgValsForCompanyName(coName)
_Unit = argVals(0)
_MemberNo = argVals(1)
_CustNo = argVals(2)
Using conn As New SqlConnection(connStr), _
cmd As New SqlCommand(upd8DML, conn)
cmd.Parameters.Add("@Unit", SqlDbType.VarChar, 50).Value = _Unit
cmd.Parameters.Add("@MemberNo", SqlDbType.VarChar, 50).Value = _MemberNo
cmd.Parameters.Add("@CustNo", SqlDbType.VarChar, 50).Value = _CustNo
conn.Open
cmd.ExecuteScalar()
End Using
End If
End If
Next
End Sub
Protected Function GetArgValsForCompanyName(coName As String) As String
Dim args(2) As String
Dim sqlConnection1 As New SqlConnection("SERVER=PLATYPUS42;DATABASE=duckbilldata;UID=salinger;PWD=dresdenoflions")
Dim cmd As New SqlCommand
Dim reader As SqlDataReader
cmd.CommandText = "select Unit, MemberNo, CustNo from Customers WHERE CompanyName = @CoName"
cmd.CommandType = CommandType.Text
cmd.Parameters.Add("@CoName", SqlDbType.VarChar, 50).Value = coName
cmd.Connection = sqlConnection1
sqlConnection1.Open()
sqlConnection1.Open()
reader = cmd.ExecuteReader()
If reader.HasRows Then
args(0) = reader.Item(0).ToString()
args(1) = reader.Item(1).ToString()
args(2) = reader.Item(2).ToString()
End If
reader.Close()
sqlConnection1.Close()
Return args
End Function
我在这里弄错了什么?如何从函数中传回一个字符串数组,然后将这些val存储在三个变量(dims)中?
注意:如果我尝试声明这样的函数:
Protected Function GetArgValsForCompanyName(coName As String) As String(2)
...我明白了,“数组边界不能出现在类型说明符中”
答案 0 :(得分:2)
你的问题似乎是你有像这样的功能签名
Protected Function GetArgValsForCompanyName(coName As String) As String
应该是这样的
Protected Function GetArgValsForCompanyName(coName As String) As String()
返回类型末尾的()声明它将是一个字符串数组,而不仅仅是一个字符串。