Function and sub Arguments

Hi

1:
Is it possible in VB to send a sub or a function
a different nomber of arguments? Like in C.
F.E.

DoThis(1, 2, 3)
DoThis(1, 2)

Usualy i get the error "Argument not optional"


2:
Is it possible to make a construction function for an object?

F.E.
private X as new clsX(1,2,3)

Thanks a lot

   Amitai
LVL 1
mAmitaiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cody76589Commented:
0
mcriderCommented:
You need to define the function with the Optional keyword to define optional arguments... For example:


   Sub MySub(Arg1 as string, Optional Arg2 as string)


With this, Arg1 is required, and Arg2 is optional...

Once you use the keyword "Optional", you can only specify other optional arguments....


Cheers!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
alokanantCommented:
1.
yes
u can pass variable number of arguments, by either using default arguments or using param array.

regarding using default arguments, u can define the function as -

private function XYZ(Byval arg1 as integer, Byval optional arg2 as string = "yyy")

'if arg2 is not passed it shall, e.g. by calling XYZ(1), then the function shall assume the value to be "yyy"

regarding using param array keyword, u pass the arguments as an array, first element of which gives the number of arguments.

for details see books on line.

2.
A constructor cannot be defined in VB, in the way it is defined in C++. Of course u can define a function or method which does the same job, but that has to be called explicitly after creating an object of the class.

hth
alok.
0
mAmitaiAuthor Commented:
Cool
however mcrider you were to trigger happy to anser and did'nt answer the second question.
therefore you only geta a B!
0
mcriderCommented:
Sorry, forgot about the 2nd question... Thanks for the point!


Cheers!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.