none
Value of type 'String' cannot be converted to '1-dimensional array of String'

    Question

  • Hello, I am using VB 2008,  I am working with arrays and I am stuck.  I am passing an array of strings to a function, and I get this error. Value of type 'String' cannot be converted to '1-dimensional array of String'.   code is below.

    ' returns the sum of quarters for the total year revenue

     

    Private Function SumArray(ByRef strRevenue() As String) As Decimal

     

    Dim iIndex As Integer

     

    Dim dDecimalStorage As Decimal

     

    Do While iIndex < strRevenue.Length AndAlso _

    IsValidRevenue(iIndex, strRevenue(iIndex)) <== this is where the error is

    dDecimalStorage += Convert.ToDecimal(strRevenue(iIndex))

    iIndex += 1

     

    Loop

     

    Return dDecimalStorage

     

    End Function

    Next is the function that the arry is passed to.

     

    Private Function IsValidRevenue(ByVal iIndex As Integer, ByRef strRevenue() As String) As Boolean

     

    'Dim index As Integer

     

    Dim dRevenue As Decimal = 0

     

    Const c_strErrMessage As String = "Enter a number between 100000 and 10000000"

     

    Try

    dRevenue = Convert.ToDecimal(strRevenue(iIndex))

     

    If dRevenue >= 100000 AndAlso dRevenue <= 10000000 Then

    return true

    Friday, April 09, 2010 11:51 PM

Answers

  • Hi R_Portland ,

    To pass the entire array just use the array name.>>

     

    IsValidRevenue(iIndex, strRevenue)

     

    Do you actual want to pass an entire array of TYPE string to your Function or just a single STRING ?

    To pass just a single STRING remove the rounded brackets a.k.a parentheses.>>

     

    Private Function IsValidRevenue(ByVal iIndex As Integer, ByRef strRevenue As  String) As Boolean

     

     

    Just so you know () after a variable name or its type indicates a one dimensional array.

    ( , ) a 2 dimensional array

    ( , , ) a 3 dimensional array and so on.

    E.G:

     

    Dim aOneD_array() As String
    Dim a2dArray(,) As String
    Dim a3dArray(,,) As String
    
    'similar to.>>
    
    Dim oneDimensionalArray As String()
    Dim twoDimensionalArray As String(,)
    Dim threeDimensionalArray As String(,,)
    
    'The same applies with the parameter section of a Function.>>
    
    Public Function SomeFunction1(ByRef a1dArray() As String)
    
    End Function
    
    'similar to.>>
    Public Function SomeFunction2(ByRef a1dArray As String()  )
    
    End Function
    
    'Note you can only add the parentheses to indicate an array
    'after a variable name OR its TYPE, not both.
    

     

    The example Functions, shown above called SomeFunction1 and SomeFunction2 expect a 1 dimensional array of type STRING.

    Do you need to change the array that you pass to the Function too?

     

    Regards,

    John

     


    Please see this thread for Vb.Net learning links.>> http://social.msdn.microsoft.com/Forums/en/vbgeneral/thread/549c8895-6780-42f8-878f-2138214fdeb4
    • Marked as answer by R_Portland Saturday, April 10, 2010 3:18 PM
    Saturday, April 10, 2010 12:10 AM

All replies

  • Hi R_Portland ,

    To pass the entire array just use the array name.>>

     

    IsValidRevenue(iIndex, strRevenue)

     

    Do you actual want to pass an entire array of TYPE string to your Function or just a single STRING ?

    To pass just a single STRING remove the rounded brackets a.k.a parentheses.>>

     

    Private Function IsValidRevenue(ByVal iIndex As Integer, ByRef strRevenue As  String) As Boolean

     

     

    Just so you know () after a variable name or its type indicates a one dimensional array.

    ( , ) a 2 dimensional array

    ( , , ) a 3 dimensional array and so on.

    E.G:

     

    Dim aOneD_array() As String
    Dim a2dArray(,) As String
    Dim a3dArray(,,) As String
    
    'similar to.>>
    
    Dim oneDimensionalArray As String()
    Dim twoDimensionalArray As String(,)
    Dim threeDimensionalArray As String(,,)
    
    'The same applies with the parameter section of a Function.>>
    
    Public Function SomeFunction1(ByRef a1dArray() As String)
    
    End Function
    
    'similar to.>>
    Public Function SomeFunction2(ByRef a1dArray As String()  )
    
    End Function
    
    'Note you can only add the parentheses to indicate an array
    'after a variable name OR its TYPE, not both.
    

     

    The example Functions, shown above called SomeFunction1 and SomeFunction2 expect a 1 dimensional array of type STRING.

    Do you need to change the array that you pass to the Function too?

     

    Regards,

    John

     


    Please see this thread for Vb.Net learning links.>> http://social.msdn.microsoft.com/Forums/en/vbgeneral/thread/549c8895-6780-42f8-878f-2138214fdeb4
    • Marked as answer by R_Portland Saturday, April 10, 2010 3:18 PM
    Saturday, April 10, 2010 12:10 AM
  • Hi again,

    Why don't you Sum your array in one Function instead of having one Function call another Function?

    Is the array that is passed to SumArray full of Decimal values in STRING format?

     

    Please try this SumArray Function with one button on a Form.>>

     

    Option Strict On
    Public Class Form1
    
        Dim rand As New Random
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim doubleList As New List(Of String)
    
            For index As Integer = 1 To 100
                doubleList.Add(rand.NextDouble.ToString)
            Next
    
            Dim doubleValuesAsStringArray() As String = doubleList.ToArray
    
            Dim result As Decimal = SumArray(doubleValuesAsStringArray)
    
            MessageBox.Show("Total of 100 random values = " & result.ToString)
    
        End Sub
    
        Public Function SumArray(ByVal someArray() As String) As Decimal
    
            Dim returnValue As Decimal = 0
            Dim parsedValue As Decimal = 0
    
            If Not (someArray Is Nothing) = True Then
                For index As Integer = 0 To someArray.GetUpperBound(0)
                    Decimal.TryParse(someArray(index), parsedValue)
                    returnValue += parsedValue
                Next
                Return returnValue
            Else
                Return 0
            End If
    
        End Function
    
    End Class

     

    Regards,

    John

     


    Please see this thread for Vb.Net learning links.>> http://social.msdn.microsoft.com/Forums/en/vbgeneral/thread/549c8895-6780-42f8-878f-2138214fdeb4
    Saturday, April 10, 2010 12:35 AM
  • Hi again,

    Why don't you Sum your array in one Function instead of having one Function call another Function?

     

    hi John :)

    My questions would be: why are you even using a Function for this? And why an array of String and not an array of Decimal?

    John shows 2 good things in his code: use a List(Of T) instead of an array, and do your validating using Double.TryParse before adding to the List (or array)

    then to get the Sum of the List, just use the...TaDa...Sum Function (note that an array also has this Function available). Although I'm not sure how you are adding values to the array, take a look at this in addition to John's. This requires a Button, a TextBox to enter the values into and a Label to Display the Sum.

    Dim lstDbl As New List(Of Double)
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim newDbl As Double
            If Double.TryParse(TextBox1.Text, newDbl) Then
                lstDbl.Add(newDbl)
            Else
                MessageBox.Show("Invalid entry")
            End If
            Label1.Text = "The Sum Is: " & lstDbl.Sum.ToString
        End Sub
    Saturday, April 10, 2010 4:58 AM
  • Although I'm not sure how you are adding values to the array.

    Hi Joe,

    The ToArray method will put a List into an array.

    The reverse to put an array into a List is ToList .

     

    Dim doubleValuesAsStringArray() As String = doubleList.ToArray
    'The reverse ToList method.>>
    doubleList = doubleValuesAsStringArray.ToList

     

    To answer your other questions, I was not aware of the Sum method for an array or a List.

    That is something else that is "new" to me, so thank you for your post.    :-)   :-D

    The OP is using an array of STRING.

     

    Regards,

    John

     


    Please see this thread for Vb.Net learning links.>> http://social.msdn.microsoft.com/Forums/en/vbgeneral/thread/549c8895-6780-42f8-878f-2138214fdeb4
    Saturday, April 10, 2010 2:58 PM
  • Hey John

    Sorry, it looked like I was questioning your code - which I wasn't :(

    I should have put "my questions to the OP would be..."

    Because I just wasn't sure how the array was being populated - one number at a time through some type of UI, reading from a textfile or database...

    Mainly I wanted to point out the validation should be done before adding to the List, not sending the items to a Function, and the OP can use the List.Sum function instead of iterating through each item of the List or array and adding it to a "Total" variable

    Saturday, April 10, 2010 3:19 PM
  • Thank you everybody

     I just needed to remove the (iIndex) part

    IsValidRevenue(iIndex, strRevenue(iIndex))

    IsValidRevenue(iIndex, strRevenue)

    Saturday, April 10, 2010 3:24 PM
  • Hey John

    Sorry, it looked like I was questioning your code - which I wasn't :(

    Hi Joe,

    No worries, I was wondering, to whom,or to what you were referring.

    Anyway, the OP, R_Portland seems to have sussed it from my earlier post. Another happy bunny!!  :-D    :-)

     

    To R_Portland ,

    Thanks for your question.

     

    Regards,

    John

     


    Please see this thread for Vb.Net learning links.>> http://social.msdn.microsoft.com/Forums/en/vbgeneral/thread/549c8895-6780-42f8-878f-2138214fdeb4
    Saturday, April 10, 2010 3:36 PM