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?
 
mcriderConnect With a Mentor Commented:
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
 
cody76589Commented:
0
 
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
All Courses

From novice to tech pro — start learning today.