-2

I'm trying to get three string values from a query, but this call to the function:

argVals = GetArgValsForCompanyName(coName)

...fails to compile with the err msg, "Value of type 'String' cannot be converted to '1-dimensional array of String'."

Nor does this line at the end of that function compile:

Return args

...which fails with, "Value of type '1-dimensional array of String' cannot be converted to 'String'."

I am declaring argVals like so, as an array of string of three vals:

Dim argVals(2) As String

...and the function like so:

Protected Function GetArgValsForCompanyName(coName As String) As String

For more context, here is the pertinent code in its (I think) entirety:

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

What am I getting wrong here? How can I pass back an array of string from a function, and then store those vals in three vars (dims)?

NOTE: If I try to declare the function like so:

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

...I get, "Array bounds cannot appear in type specifiers"

Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • Possible duplicate of [Declaring and initializing a string array in VB.NET](http://stackoverflow.com/questions/5262577/declaring-and-initializing-a-string-array-in-vb-net) – Filburt Mar 20 '17 at 23:31
  • 4
    `GetArgValsForCompanyName(coName As String) As String()` then `Return args`. Pretty much the same as c# except using `()` as the array designator – Ňɏssa Pøngjǣrdenlarp Mar 20 '17 at 23:57

1 Answers1

2

Your problem seems to be that you have the function signiture like so

Protected Function GetArgValsForCompanyName(coName As String) As String

When it should be like this

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

The () at the end of the return type states that it will be a string array, and not just a string.

Sasha
  • 1,674
  • 1
  • 16
  • 23