Can I create a sub with a variable number of paramters?

I have a hash that is based on the CollectionBase class:

public class myclass
inherits system.collections.collectionbase

private the_hash as new hashtable ' Internal storage

public sub add( byval thing as whatever, byval name as string)
    me.list.add(thing)
    the_hash.add(thing,thing.name)
end sub

So to use it I say eg

dim h as new myclass
h.add(thing1)
h.add(thing2)
h.add(thing3)

What I would like to do is have a constructor with a variable number of parameters that I could use like this:

dim h as new myclass(thing1, thing2, thing3)

Can I do this?
   
kilgore666Asked:
Who is Participating?
 
S-TwilleyConnect With a Mentor Commented:
If you want ANY number of parameters... use the ParamArray keyword..

From the help file....


Sub P(ByVal D As Date, ByVal ParamArray C() As Char)
' The preceding definition is equivalent to the following overloads:
'    Overloads Sub P(ByVal D As Date, ByVal C() As Char)
'    Overloads Sub P(ByVal D As Date)
'    Overloads Sub P(ByVal D As Date, ByVal C As Char)
'    Overloads Sub P(ByVal D As Date, ByVal C1 As Char, ByVal C2 As Char)
' And so on, with an additional Char argument in each successive overload.

So you can call Sub P  with a date followed by any number of arguments of type Char
0
 
Brian CroweDatabase AdministratorCommented:
just use an array of "thing" as your parameter
0
 
kilgore666Author Commented:
Not sure what you mean. My own solution uses a hash rather than an array.

What are you suggesting the declaration of New would be and how would you call it with 2 parameters, and with three parameters (eg)
0
 
S-TwilleyCommented:
any example using paramarray...

Sub P(ByVal D As Date, ByVal ParamArray C() As Char)
    Dim xChar as Char

    For Each xChar in C
         Msgbox(xChar.ToString)
    Next
End Sub
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.