Provide default values for optional parameters
You can also specify default values for optional parameters. In the following example, if optional parameters are not passed to the function procedure, a default value is returned.
SubListText(xAsString,OptionalyAs_
Integer=12345)
List1.AddItemx
List1.AddItemy
EndSub
PRivateSubCommand1_Click()
strName=yourname 'The second parameter was not supplied.
CallListText(strName) 'Add "yourname" and "12345".
EndSub
Using an Indeterminate Number of Parameters In general, the number of parameters in a procedure call should equal the number of parameters in the procedure description. You can use the ParamArray keyword to indicate that the procedure will accept any number of parameters. So you can write the Sum function to calculate the sum like this:
DimxAsInteger
DimyAsInteger
DimintSumAsInteger
SubSum(ParamArrayintNums())
ForEachxInintNums
y=yx
Nextx
intSum=y
EndSub
PrivateSubCommand1_Click()
Sum1,3,5,7,8
List1.AddItemintSum
EndSub
Create simple statements with named parameters
For many built-in functions, statements, and methods, Visual Basic provides named parameter methods to quickly pass parameter values. For named parameters, any number of parameters can be provided in any order by assigning values to the named parameters. To do this, type the named argument, followed by a colon, an equal sign, and the value (MyArgument:=SomeValue). These assignments can be arranged in any order, separated by commas. Note that the order of the parameters in the following example is opposite to the order of the required parameters:
FunctionListText(strNameAsString,OptionalstrAddressAsString)
List1.AddItemstrName
List2.AddItemstrAddress
EndSub
PrivateSubCommand1_Click()
ListTextstrAddress:=12345,strName:=YourName
EndSub
The above is more useful if the procedure has several optional parameters that do not always have to be specified.
Confirm support for named parameters
To determine which functions, statements, and methods support named parameters, use the AutoQuickInfo function in the Code window, check the Object Browser, or consult the language reference. Keep the following points in mind when using named parameters:
Methods of objects in the Visual Basic (VB) object library do not support named parameters. All language keywords in the Visual Basic for Applications (VBA) object library support named parameters.
In the syntax, named parameters are shown in bold and italics. All other parameters are in italics only.
It is important to note that when using named parameters, you cannot omit the input of required parameters. Only optional parameters can be omitted. For Visual Basic (VB) and Visual Basic for Applications (VBA) object libraries, the Object Browser dialog box encloses optional parameters in square brackets [].
See ByVal, ByRef, Optional, and ParamArray in the language reference for details .
Control structure overview
With the control structure, the flow of program execution can be controlled. If control flow statements are not checked, the program runs through these statements from left to right and top to bottom. Some simple programs can be written with only one-way flow, and some flow can be controlled by operator precedence, but the effectiveness and usefulness of any programming language comes from its ability to change the order of statements through structures and loops.
decision structure
Visual Basic procedures can test conditional expressions and then perform different actions based on the test results.
The decision structures supported by Visual Basic are:
1.If...Then
2.If...Then...Else
3.SelectCase
If...Then
Use the If...Then structure to conditionally execute one or more statements. Both single-line syntax and multi-line block syntax can be used:
IfconditionThenstatement
IfconditionThen
statements
EndIf
condition is usually a comparison, but it can be any expression that computes a numeric value. Visual Basic interprets this value as True or False: a zero value is False, and any nonzero value is considered True. If condition is True, Visual Basic executes all statements following the Then keyword. You can conditionally execute a statement using single-line or multi-line syntax (the following two examples are equivalent):
IfanyDate<NowThenanyDate=Now
IfanyDate<NowThen
anyDate=Now
EndIf
Note: The single-line format of If...Then does not use the EndIf statement. If you want to execute multiple lines of code when condition is True, you must use the multi-line block If...Then...EndIf syntax.
IfanyDate<NowThen
anyDate=Now
Timer1.Enabled=False 'Timer control is disabled.
EndIf
If...Then...Else
Define several statement blocks with If...Then...Else blocks and execute one of the statements:
Ifcondition1Then
[statementblock-1]
[ElseIfcondition2Then
[statementblock-2]]...
[Else
[statementblock-n]]
EndIf
VisualBasic first tests condition1. If it is False, Visual Basic tests condition2, and so on, until it finds a condition that is True. When it finds a condition that is True, Visual Basic executes the corresponding statement block and then executes the code following EndIf. As an option, you can include an Else statement block that Visual Basic executes if none of the conditions are True.
If...Then...ElseIf is just a special case of If...Then...Else. Note that you can use any number of ElseIf clauses, or none at all. There can be an Else clause regardless of whether there is an ElseIf clause.
For example, an application can take an action based on which control in an array of menu controls is clicked:
PrivateSubmnuCut_Click(IndexAsInteger)
IfIndex=0Then '"Cut" command.
CopyActiveControl 'Call a common procedure.
ClearActiveControl
ElseIfIndex=1Then '"Copy" command.
CopyActiveControl
ElseIfIndex=2Then 'Clear command.
ClearActiveControl
Else 'Paste command.
PasteActiveControl
EndIf
EndSub
Note that you can always add more ElseIf blocks to the If...Then structure. However, this structure is tedious to write when each ElseIf compares the same expression to a different numerical value. In this case you can use the SelectCase decision structure.
For detailed information, please refer to "If...Then...Else Statement" in "VisualBasic6.0 Language Reference Manual".
SelectCase
VisualBasic provides the SelectCase structure to replace If...Then...Else, so that one of multiple statement blocks can be selectively executed. The capabilities of the SelectCase statement are similar to the If...Then...Else statement, but in the case of multiple selections, the SelectCase statement makes the code more readable.
SelectCase handles a test expression above the structure and evaluates it only once. Visual Basic then compares the value of the expression to the value of each Case in the structure. If they are equal, the statement block associated with the Case is executed.
SelectCasetestexpression
[Caseexpressionlist1
[statementblock-1]]
[Caseexpressionlist2
[statementblock-2]]
.
.
.
[CaseElse
[statementblock-n]]
EndSelect
Each expressionlist is a list of one or several values. If there are multiple values in a list, separate the values with commas. Each statementblock contains zero or more statements.
If more than one Case matches the test expression, only the statement block associated with the first matching Case is executed. If no value in the expression list matches the test expression, Visual Basic executes the statement in the CaseElse clause (this is optional).
For example, suppose you want to add a command to the Edit menu in the If...Then...Else example. You can add an ElseIf clause for this, or use SelectCase to write a function:
PrivateSubmnuCut_Click(IndexAsInteger)
SelectCaseIndex
Case0 'Cut' command.
CopyActiveControl 'Call a common procedure.
ClearActiveControl
Case1 '"Copy" command.
CopyActiveControl
Case2 '"Clear" command.
ClearActiveControl
Case3 'Paste command.
PasteActiveControl
CaseElse
frmFind.Show 'Show the found dialog box.
EndSelect
EndSub
Note that the SelectCase structure evaluates the expression at the beginning each time. The If...Then...Else structure evaluates a different expression for each ElseIf statement. You can replace an If...Then...Else structure with a SelectCase structure only if the If statement and each ElseIf statement evaluate the same expression.
->