VB variables, constants and data types and process overview (11)

Source: Internet
Author: User
Tags integer

Provides a default value for an optional parameter

You can also specify a default value for an optional parameter. In the following example, if an optional parameter is not passed to a Function procedure, a default value is returned.
Sub Listtext (x as String, Optional y as _
Integer = 12345)
List1.AddItem x
List1.AddItem y
End Sub

Private Sub Command1_Click ()
StrName = "Yourname" does not provide a second argument.
Call Listtext (strName) ' Add "yourname" and "12345".
End Sub
Use an indefinite number of parameters in general, the number of parameters in a procedure call should equal the number of parameters described in the procedure. You can use the ParamArray keyword to indicate that the procedure will accept any number of arguments. You can then write the SUM function that calculates the sum:
Dim x as Integer
Dim y as Integer
Dim Intsum as Integer

Sub Sum (ParamArray intnums ())
For each x in Intnums
y = y + x
Next x
Intsum = y
End Sub

Private Sub Command1_Click ()
Sum 1, 3, 5, 7, 8
List1.AddItem intsum
End Sub

To create a simple statement with a named parameter
For many built-in functions, statements, and methods, Visual Basic provides named parameter methods to quickly pass parameter values. For named arguments, you can provide any number of parameters in any order by assigning values to named arguments. To do this, type a named parameter followed by a colon, an equal sign, and a value (myargument: = "somevalue") that can be assigned in any order, separated by commas. Note that the order of the parameters in the following example is the opposite of the order of the parameters you want:
Function Listtext (StrName As String, Optional straddress as String)
List1.AddItem StrName
List2.additem straddress
End Sub

Private Sub Command1_Click ()
Listtext straddress:= "12345", strname:= "Your Name"
End Sub
This is more useful if the procedure has several optional parameters that you do not always need to specify.

Determining support for named parameters
To determine which functions, statements, and methods support named parameters, check the Object Browser using the Autoquickinfo feature in the Code window, or see the language reference. The following points are noted when using named parameters:
Named parameters are not supported by methods of objects in the Visual Basic (VB) object library. All language keywords in the VisualBasic for Applications (VBA) object library support named parameters.
In syntax, named arguments are expressed in bold and italic characters. All other parameters are indicated in italics only.
You cannot omit the input of the required parameters when using named parameters. You can omit only optional parameters. For Visual Basic (VB) and Visual Basic for Applications (VBA) object libraries, the Object Browser dialog box encloses optional parameters in square brackets [].
For more information, see "ByVal", "ByRef", "Optional", and "ParamArray" in the language reference.

Control Structure Overview
Procedures for controlling the execution of a control structure are available. If the control flow statement is not checked, the program runs through the statements from left to right and from top to bottom. Some simple programs can be written using only one-way processes, and some processes can be controlled by the precedence of operators, but the validity and usefulness of any programming language is due to its ability to change the order of statements through structure and loop.

Decision structure
The Visual Basic process can test the conditional 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.Select case
If ... Then
Use If ... The Then structure conditionally executes one or more statements. Both Single-line syntax and multiline block syntax can be used:
If condition Then Statement
If condition Then
Statements
End If
Condition is usually a comparison, but it can be any expression that evaluates to a value. Visual Basic interprets this value to be true or false: A value of zero is false, and any non-0 values are treated as true. If condition is True, Visual Basic executes all statements after the Then keyword. You can conditionally execute a statement using single or multiline syntax (the following two examples are equivalent):
If Anydate < now Then anydate = Now

If Anydate < now Then
Anydate = Now
End If
Note: If ... The Then line format does not use the end If statement. If you want to execute multiple lines of code when condition is True, you must use multiple lines of block if ... Then ... End If syntax.
If Anydate < now Then
Anydate = Now
timer1.enabled = False ' Timer control failure.
End If

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.