Passing Array as Arguement

tjams

New member
Joined
Sep 5, 2011
Messages
1
Programming Experience
1-3
:dispirited:I m a student and new to VB
I m triyng to pass an array as parrameters
And the compiler says I havent declared the array my coding is as below
Please help me
[XCODE]Sub Main()
'Fill array
Dim MyArray(0 To 9) As String
For lLoop As Integer = 1 To UBound(MyArray)
Console.Write(
"Enter First Name " & lLoop & ": ")
MyArray(lLoop) = Console.ReadLine()
Next lLoop
SortNames(MyArray)
Console.ReadLine()
End Sub
Public Function SortNames(ByVal ParamArray Arg() As String) As String
Dim lLoop As Long
Dim str1 As String
Dim str2 As String
'Sort array
For lLoop = 0 To UBound(MyArray) ' When I call This Array here I get a declaration error
For lLoop2 = lLoop To UBound(MyArray)
If UCase(MyArray(lLoop2)) < UCase(MyArray(lLoop)) Then
str1 = MyArray(lLoop)
str2 = MyArray(lLoop2)
MyArray(lLoop) = str2
MyArray(lLoop2) = str1
End If
Next lLoop2
Next lLoop
'Output sorted array
For i As Integer = LBound(MyArray) To UBound(MyArray)
Console.WriteLine(MyArray(i))
Next
SortNames = lLoop
End Function
[/XCODE]
 
Within SortNames method you use the 'Arg' parameter, that represent the array object passed as argument when the method was called.
 
E.g.
Private Sub DisplayArgs(ParamArray args As String())
    For Each arg In args
        MessageBox.Show(arg)
    Next
End Sub
Sample usage:
DisplayArgs("one discrete string")
DisplayArgs("first of multiple discrete strings", "second of multiple discrete strings")

Dim args1 As String() = {"one string array element"}
Dim args2 As String() = {"first of multiple string array elements", "second of multiple string array elements"}

DisplayArgs(args1)
DisplayArgs(args2)
 
Back
Top