[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2005-04-15
4
Medium Priority
?
156 Views
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)
    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?
   
0
Comment
Question by:kilgore666
  • 2
4 Comments
 
LVL 34

Expert Comment

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

Author Comment

by:kilgore666
ID: 13791812
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
 
LVL 12

Accepted Solution

by:
S-Twilley earned 2000 total points
ID: 13791820
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
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13791839
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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.
Loops Section Overview
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month19 days, 20 hours left to enroll

873 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