VBGood网站全文搜索 Google

首页 - 经验之谈 - 用ParamArray接收任意个参数
发表评论(0)作者:不详, 平台:VB6.0+Win98, 阅读:12706, 日期:2000-10-20
Use ParamArray to Accept an Arbitrary Number of Parameters
Submitted by Michael C. Amundsen, mike_tips@amundsen.com, http://www.amundsen.com
You can use the ParamArray keyword in the declaration line of a method to create a subroutine or function that accepts an arbitrary number of parameters at runtime. For example, you can create a method that will fill a list box with some number of items even if you do not know the number of items you will be sent. Add the method below to a form:

Public Sub FillList(ListControl As ListBox, ParamArray Items())
    Dim i As Variant
    With ListControl
        For Each i In Items
            .AddItem i
    End With
End Sub

Note that the ParamArray keyword comes BEFORE the parameter in the declaration line. Now add a list box to your form and a command button. Add the code below in the "Click" event of the command button.

Private Sub Command1_Click()
    FillList List1, "TiffanyT", "MikeS", "RochesterNY"
End Sub