How return an array value in a function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Woody Splawn

    How return an array value in a function

    How would I pass an array back to a sub routine from a function? That is, I
    have a function that looks like this

    Public Function arrayTest() As Array
    Dim states() As String = { _
    "AZ", "CA", "WA" _
    }
    Return states
    End Function

    I have a button with code like this:

    Dim states() As String
    states = arrayTest()

    However, I get a squiqqly under the "ArrayTest( )" saying , "Option strict on
    disallows implicit conversions from System.array to 1-demsional array of
    string.

    I would like to be able to write the states code once and have it called
    from a variety of places. My first thought is to write it in a function,
    and call the values when needed from the function. If this is the best way
    of doing this, how do I do it and if not, what is better?



  • Jay B. Harlow [MVP - Outlook]

    #2
    Re: How return an array value in a function

    Woody,
    You either need to define your function as returning an array of strings. or
    you need to cast the value returned to an array of strings.

    Return an array of strings:[color=blue]
    > Public Function arrayTest() As String()
    > Dim states() As String = { _
    > "AZ", "CA", "WA" _
    > }
    > Return states
    > End Function[/color]


    Cast the value returned:[color=blue]
    > Public Function arrayTest() As Array[/color]
    [color=blue]
    > Dim states() As String
    > states = directcast(arra yTest(), string())[/color]

    I prefer the first over the second, however casting the value is useful
    also:

    Hope this helps
    Jay


    "Woody Splawn" <woody@splawns. com> wrote in message
    news:%23q7F9k5h DHA.1180@TK2MSF TNGP12.phx.gbl. ..[color=blue]
    > How would I pass an array back to a sub routine from a function? That is,[/color]
    I[color=blue]
    > have a function that looks like this
    >
    > Public Function arrayTest() As Array
    > Dim states() As String = { _
    > "AZ", "CA", "WA" _
    > }
    > Return states
    > End Function
    >
    > I have a button with code like this:
    >
    > Dim states() As String
    > states = arrayTest()
    >
    > However, I get a squiqqly under the "ArrayTest( )" saying , "Option strict[/color]
    on[color=blue]
    > disallows implicit conversions from System.array to 1-demsional array of
    > string.
    >
    > I would like to be able to write the states code once and have it called
    > from a variety of places. My first thought is to write it in a function,
    > and call the values when needed from the function. If this is the best[/color]
    way[color=blue]
    > of doing this, how do I do it and if not, what is better?
    >
    >
    >[/color]


    Comment

    • Herfried K. Wagner [MVP]

      #3
      Re: How return an array value in a function

      "Woody Splawn" <woody@splawns. com> scripsit:[color=blue]
      > How would I pass an array back to a sub routine from a function? That is, I
      > have a function that looks like this
      >
      > Public Function arrayTest() As Array
      > Dim states() As String = { _
      > "AZ", "CA", "WA" _
      > }
      > Return states
      > End Function
      >
      > I have a button with code like this:
      >
      > Dim states() As String
      > states = arrayTest()
      >
      > However, I get a squiqqly under the "ArrayTest( )" saying , "Option strict on
      > disallows implicit conversions from System.array to 1-demsional array of
      > string.[/color]

      Why not declare the function like this?

      \\\
      Public Function ArrayTest() As String()

      Comment

      • Fergus Cooney

        #4
        Re: How return an array value in a function

        Hi Woody,

        The syntax that you are after looks like this:
        Public Function States() As String()
        Return New String() { "CA", "FL" }
        End Function

        This creates a new copy of the array for each 'user' that calls the
        function. If any of the users 'tampers' with the array it will have no effect
        on the next user.


        The simplest version
        Public Module Things
        Public StateCodes() As String = New String() { "CA", "FL" }
        End Module

        And you can also do this:
        Private m_StateCodes() As String = New String() { "CA", "FL" }
        Public Function States() As String()
        Return m_StateCodes
        End Function

        These are more efficient in that the array is only created once, but if
        any user changes a State's code, this will effect all subsequent users. The
        second version adds an interface between the user and the array which would
        allow you to change how m_StateCodes is implemented.

        Then there's
        Private m_StateCodes() As String = New String() { "CA", "FL" }
        Public ReadOnly Property States As String()
        Get
        Return m_StateCodes
        End Get
        End Function

        This one also allows the array to be tampered with. This is despite the
        ReadOnly, as that refers to the Property - not the array contents.

        Another version:
        Private m_StateCodes() As String = New String() { "CA", "FL" }
        Public ReadOnly Property StateCode (Index As Integer) As String
        Get
        Return m_StateCodes (Index) 'Add error checking as required.
        End Get
        End Function

        This will provide the individual states and not allow them to be altered.
        This means a Property access for each State code which is less efficient than
        a direct array access. However, I believe that the JIT compiler can put the
        code for small function/property calls inline which removes the call-return
        overhead..

        Which is best depends on how you are using this array. If you want
        occasional access then I would suggest the last version - a Property for
        accessing individual State codes. If you want to process the list of State
        code, eg in creating a table or filling a ListBox, etc, then I would suggest
        the first version - a tamper-proof function which provides the whole list.

        The versions in between are just for background.

        Regards,
        Fergus


        Comment

        • Gary Owsiany

          #5
          Re: How return an array value in a function

          In the VB.Net online documentation, look in UserControl class. One of the
          uses for defining a UserControl is: "Another efficient use of the user
          control is to simply preload a ComboBox or ListBox with static items you
          commonly use in almost every application; some examples of this are
          countries/regions, cities, states, and office locations."
          Gary
          "Woody Splawn" <woody@splawns. com> wrote in message
          news:%23q7F9k5h DHA.1180@TK2MSF TNGP12.phx.gbl. ..[color=blue]
          > How would I pass an array back to a sub routine from a function? That is,[/color]
          I[color=blue]
          > have a function that looks like this
          >
          > Public Function arrayTest() As Array
          > Dim states() As String = { _
          > "AZ", "CA", "WA" _
          > }
          > Return states
          > End Function
          >
          > I have a button with code like this:
          >
          > Dim states() As String
          > states = arrayTest()
          >
          > However, I get a squiqqly under the "ArrayTest( )" saying , "Option strict[/color]
          on[color=blue]
          > disallows implicit conversions from System.array to 1-demsional array of
          > string.
          >
          > I would like to be able to write the states code once and have it called
          > from a variety of places. My first thought is to write it in a function,
          > and call the values when needed from the function. If this is the best[/color]
          way[color=blue]
          > of doing this, how do I do it and if not, what is better?
          >
          >
          >
          >[/color]


          Comment

          Working...