Optional VB. NET Parameters

Source: Internet
Author: User

You can specify the process parameters that are optional and do not need to provide them with variables when calling the process. In the process definition, "optional parameters" are defined by keywordsOptional. The following rules apply:

  • The default value must be specified for each optional parameter in the process definition.

  • The default value of an optional parameter must be a constant expression.

  • In the process definition, each parameter following the optional parameters must also be optional.

The following syntax shows the process declaration with optional parameters:

Code:
  1. Sub name (byval parameter 1 as Data Type 1, optional byval Parameter

 

Call the process with optional parameters

During running, the process cannot detect whether the specified parameter is omitted or whether the Call Code explicitly provides the default value. If you need to clarify this, you can set an impossible value as the default value. The following process defines the optional parameter office and tests its default value qjz to check whether it has been omitted in the call:

Code:
  1. Sub every Y (byval company as string, optional byval office as string = "qjz ")
  2. If office = "qjz" then
  3. Debug. writeline ("office not supplied -- using headquarters ")
  4. Office = "headquarters"
  5. End if
  6. 'Insert code to your y headquarters or specified office.
  7. End sub

If the optional parameter is an imageStringSuch a reference type can be used as long as it is not the expected value of the variable.NothingAs the default value.

Optional parameter and overload

Another way to define a process with optional parameters is to use overload. If there is an optional parameter, you can define two overloaded versions of the process. One accepts this parameter, and the other does not include this parameter. This method becomes more complex as the number of optional parameters increases. However, the advantage of this is that you can fully determine whether each optional parameter is provided by the calling program.

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.