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

Posted on 2005-04-15
Last Modified: 2010-04-23
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)
end sub

So to use it I say eg

dim h as new myclass

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?
Question by:kilgore666
    LVL 34

    Expert Comment

    by:Brian Crowe
    just use an array of "thing" as your parameter

    Author Comment

    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)
    LVL 12

    Accepted Solution

    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
    LVL 12

    Expert Comment

    any example using paramarray...

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

        For Each xChar in C
    End Sub

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now