I want to make a Function who not all parameter are needed how can I do that ?
Printable View
I want to make a Function who not all parameter are needed how can I do that ?
Example :
VB Code:
Private Function Calcu(x as Double, y as Double, z as Double) as Double Calcu = x + y + z End Function
Like if I do not put z it will work... How can I do that ?
VB Code:
Option Explicit Private Sub Command1_Click() MsgBox Calcu(1, 2) End Sub Private Function Calcu(x As Double, y As Double, Optional z As Double) As Double Calcu = x + y + z End Function
As follows:
VB Code:
Function XYZ(X As This, Y As That, [COLOR=red]Optional[/COLOR] Z As Whatever) 'Your Code End Function
Thats it, you won't have to pass z if you don't want to.
Remember the following points in case of using Optional Parameters:
- The Paramebers following the first Optional Parameter must also be optional
- You can specify the default value of the Optional Parameter, if the user doesn't Pass any Value for that
- In case of a Variant Optional Parameter, you could check to see if it was specified using the IsMissing() Function
- In case you don't specify the Default Value of other DataTypes, it will be initialized as a standard Variable of that type: Inetegers will be Initialized to zero
- In case you need to use *any* number of Parameter which is unknown at the time you write the code, you can use the ParameterArray which would allow you to pass any number of parameters to a function.
Cheers
I think 50 words per minute is not good enough. I have to be faster.
:rolleyes:
Abu haider your explication was simply excellent!!! THX YOU man !!! :)
:cool:
The only one thing I do not understand is : In case you need to use *any* number of Parameter which is unknown at the time you write the code, you can use the ParameterArray which would allow you to pass any number of parameters to a function.
:confused:
If you dont know the datatype of the para
Yes but I do not know what to do in that case :(