Passing arrays as parameters

rbulph

Junior Contributor
Joined
Feb 17, 2003
Messages
397
Is there any way that you can create an array to pass directly as a parameter? i.e. avoiding the need for the variable f in the following code and just putting the array elements in the call to the procedure:
Visual Basic:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim f() As String = {"ioj", "OIJ"}
        hioh(f)

    End Sub

    Private Sub hioh(ByVal f() As String)

    End Sub
End Class

It seems to me that there ought to be, but I can't figure out what it is.
 
Visual Basic:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

hioh("ioj", "OIJ")

End Sub

Private Sub hioh(ByVal ParamArray f() As String)

End Sub

should be what you are looking for.
 
Oh yes, of course. However it would still be useful if I could do as I was originally anticipating for the case where the procedure wants to take two string arrays, for example. You can't have two ParamArrays as parameters, because there's no way of specifying where one array ends and the next begins.
 
Maybe...
Visual Basic:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Call hioh with a newly created array...
        hioh(New String() {"ioj", "OIJ"})
 
        'Call that other method with arrays
        mnkxsjmksdx(New String() {"fe", "htr", "fdsa"}, New String() {"hgt", "htr"} )
    End Sub
 
    'Method that accepts an array
    Private Sub hioh(ByVal f() As String)
        MessageBox.Show("Recieved array with " & f.Length.ToString() & " elements.")
    End Sub
    
    Private Sub mnkxsjmksdx(ByVal a() As String, ByVal b() As String)
    End Sub
End Class

By the way, I like the mash-your-face-into-the-keyboard naming convention.
 
marble_eater said:
Maybe...
Visual Basic:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Call hioh with a newly created array...
        hioh(New String() {"ioj", "OIJ"})
 
        'Call that other method with arrays
        mnkxsjmksdx(New String() {"fe", "htr", "fdsa"}, New String() {"hgt", "htr"} )
    End Sub
 
    'Method that accepts an array
    Private Sub hioh(ByVal f() As String)
        MessageBox.Show("Recieved array with " & f.Length.ToString() & " elements.")
    End Sub
    
    Private Sub mnkxsjmksdx(ByVal a() As String, ByVal b() As String)
    End Sub
End Class

By the way, I like the mash-your-face-into-the-keyboard naming convention.

Ah yes, that does it, thanks. Contrary to appearances I do use my fingers to select the names - and I find I can do this remarkably quickly. I've also found in earlier professional experience that people who produce absolute rubbish can do it very quickly and it can be hard to criticise because nobody knows what on earth they really intended and it's far too rude to say that it's rubbish. If you're more conscientious then you have to worry about a host of details such as "'i' before 'e' except after 'c'" (!), otherwise it shows. So I use the random technique of name selection (sometimes).
 
Back
Top