2017-03-20 83 views
-2

我試圖從查詢得到三個字符串值,但這種調用該函數:我如何從一個函數返回一個字符串數組?

​​

...失敗,出現錯誤消息來編譯,「值類型的‘字符串’不能。轉換爲「字符串的1維陣列」

也不這條線在該函數的編譯的末尾:

Return args 

...其失敗,」 值類型的「1維度ar ray of String'不能轉換爲'String'。

我聲明,像這樣argVals,作爲三個瓦爾斯字符串數組:

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 

我在這裏發生了什麼問題?我怎樣才能傳回字符串數組從一個函數,然後存儲這些丘壑三瓦爾(DIMS)?

注意:如果我嘗試聲明函數像這樣:

Protected Function GetArgValsForCompanyName(coName As String) As String(2) 

...我得到的,「數組界限不能出現在類型說明符

+0

[在VB.NET中聲明和初始化字符串數組]可能的副本(http://stackoverflow.com/questions/5262577/declaring-and-initializing-a-string-array-in-vb-net) – Filburt

+4

'GetArgValsForCompanyName(coName作爲字符串)作爲字符串()''然後返回args'。幾乎相同爲c#除了使用'()'作爲數組指示符 – Plutonix

回答

2

您的問題似乎是,你有像這樣

Protected Function GetArgValsForCompanyName(coName As String) As String 

功能signiture當它應該是這樣的

Protected Function GetArgValsForCompanyName(coName As String) As String() 

的()中的返回類型的結束狀態,這將是一個字符串數組,而不僅僅是一個字符串。

相關問題