locked
Function that gives an Array as return RRS feed

  • Question

  • Dear Friends.

    I want to make a function that gives an Array of String as it return.

    Can you please let me know?  I know how to make Function but making it to give an Array of String is unknown for me... I need 9 String as the result.

    Thanks

    Kind Regards

    VKSBK


    A Real Novice Programmer !

    Wednesday, June 3, 2015 2:32 PM

Answers

  • This is an example:
     Public Function MyFunctionName() As String()
            Dim myArr As String() = {"string1", "string2", "string3", "string4", "string5", "string6", "string7", "string8", "string9"}
    
            Return myArr
        End Function
    More examples on how to populate string array.

    Fouad Roumieh

    • Proposed as answer by Blackwood Wednesday, June 3, 2015 4:19 PM
    • Marked as answer by V.K.S.B.K Wednesday, June 3, 2015 8:02 PM
    Wednesday, June 3, 2015 3:38 PM
  • You need to declare the array in the calling program.  You can assign values inside either a subprocedure or a function procedure.  Here is an example of both in a Console application:

    Module Module1
    
        Sub Main()
            Dim myarray(8) As String
            myarray(8) = retarray(myarray)      'returns the function
            For x As Integer = 0 To 8
                Console.WriteLine(myarray(x))
            Next
            Console.WriteLine()
    
            Dim newarray(8) As String
            Call subarray(newarray)             'calls the subprocedure
            For x As Integer = 0 To 8
                Console.WriteLine(newarray(x))
            Next
            Console.ReadLine()
        End Sub
    
        Function retarray(ByVal myarray() As String) As String
            For x As Integer = 0 To 8
                myarray(x) = x.ToString
            Next
            Return myarray(8)
        End Function
    
        Sub subarray(ByVal newarray() As String)
            For x As Integer = 0 To 8
                newarray(x) = x.ToString
            Next
        End Sub
    
    End Module


    Solitaire

    • Marked as answer by V.K.S.B.K Wednesday, June 3, 2015 8:02 PM
    Wednesday, June 3, 2015 6:10 PM

All replies

  • This is an example:
     Public Function MyFunctionName() As String()
            Dim myArr As String() = {"string1", "string2", "string3", "string4", "string5", "string6", "string7", "string8", "string9"}
    
            Return myArr
        End Function
    More examples on how to populate string array.

    Fouad Roumieh

    • Proposed as answer by Blackwood Wednesday, June 3, 2015 4:19 PM
    • Marked as answer by V.K.S.B.K Wednesday, June 3, 2015 8:02 PM
    Wednesday, June 3, 2015 3:38 PM
  • You need to declare the array in the calling program.  You can assign values inside either a subprocedure or a function procedure.  Here is an example of both in a Console application:

    Module Module1
    
        Sub Main()
            Dim myarray(8) As String
            myarray(8) = retarray(myarray)      'returns the function
            For x As Integer = 0 To 8
                Console.WriteLine(myarray(x))
            Next
            Console.WriteLine()
    
            Dim newarray(8) As String
            Call subarray(newarray)             'calls the subprocedure
            For x As Integer = 0 To 8
                Console.WriteLine(newarray(x))
            Next
            Console.ReadLine()
        End Sub
    
        Function retarray(ByVal myarray() As String) As String
            For x As Integer = 0 To 8
                myarray(x) = x.ToString
            Next
            Return myarray(8)
        End Function
    
        Sub subarray(ByVal newarray() As String)
            For x As Integer = 0 To 8
                newarray(x) = x.ToString
            Next
        End Sub
    
    End Module


    Solitaire

    • Marked as answer by V.K.S.B.K Wednesday, June 3, 2015 8:02 PM
    Wednesday, June 3, 2015 6:10 PM
  • What will be the input to this function ?

    Is it always 9 strings ?

    Wednesday, June 3, 2015 7:18 PM
  • Fouad,

    First I missed your intention and then I saw the answer was 100% correct. 

    I hope the OP understands that myArr with the curly bracelets, some persons are scared by it.

    :-)

    To take care there is looked twice by the OP


    Success
    Cor

    Wednesday, June 3, 2015 7:36 PM
  • Dear Friends.

    I want to make a function that gives an Array of String as it return.

    Can you please let me know?  I know how to make Function but making it to give an Array of String is unknown for me... I need 9 String as the result.

    Thanks

    Kind Regards

    VKSBK


    A Real Novice Programmer !

    As stated, it may have a return type as a String(), but maybe not - you haven't given enough to details to know.

    For example, the return type may be an instance of a class or structure - which has nine (9) string properties. The return type wouldn't be a string array though, it'd be an instance of that class or structure.

    Please explain about what's going on with the function -- what is it supposed to do? What parameter(s) will it use (if any)?

    Most likely the question has been answered by the respondents already, but as stated, maybe not.


    Still lost in code, just at a little higher level.

    :-)

    Wednesday, June 3, 2015 7:48 PM
  • Dear Friends,

    Thanks for all replies.

    I'll try and come back if I need further help....

    Thanks

    Regards

    VKSBK


    A Real Novice Programmer !

    Wednesday, June 3, 2015 8:02 PM
  • Fouad,

    First I missed your intention and then I saw the answer was 100% correct. 

    I hope the OP understands that myArr with the curly bracelets, some persons are scared by it.

    :-)

    To take care there is looked twice by the OP


    Success
    Cor

    I had that thought when posting it, that it could be misinterpreted, that's why I've appended examples link showing other ways :)

    Fouad Roumieh

    Thursday, June 4, 2015 8:31 AM