Vb. NET is how to do (vii)--optional parameters and by name Pass

Source: Internet
Author: User
The number of new postings on the. NET blog has been decreasing. I think everyone should be diligent, share their experience and ideas, do not let this blog decline.

VB supports the feature "optional parameters" starting from 4.0. That is, some of the parameters of a function or subroutine are optional and can be invoked without input. In fact, VB from the beginning of 1.0 there are some functions with optional parameters, but only to 4.0 to allow users to develop such a process themselves. In VB4, an optional parameter can have no default value, and in vb.net, if you use an optional parameter, you must have a default value. Such as

Public Sub testoptional (Optional i as Integer = 1)

End Sub

When invoked, it can be written either as testoptional (2) or as a testoptional (), and this condition parameter I automatically equals 1. If the procedure has more than one optional parameter, VB also provides a way to simplify operations-passing parameters by name. such as process

Public Sub testoptional (Optional i as Int32 = 1, Optional j as Int32 = 1, Optional k as Int32 = 1)

End Sub

If you want to specify K only, let I and J use the default values, you can use the by name pass, as follows

Testoptional (k: = 2)

And this way is not limited by the order of parameter tables

Testoptional (k: = 2, I: = 3, J: = 5)

These are indeed quite handy features, and C # does not support both of these features. Let's see how it is implemented at the IL level. The first method described above is defined in the IL as

. method public instance void Testoptional ([opt] int32 i) CIL managed
{
. param [1] = Int32 (0x00000001)
. maxstack 8

Visible, the parameter is added with the [opt] modifier, and. PARAM specifies the default value for the parameter. This is only VB can recognize the content, C # will skip them. When a call is made, VB automatically reads the default value of the. param part and passes it explicitly to the procedure if it finds that the parameter is omitted. This part is entirely handled by the compiler and has no performance penalty, and is exactly the same as passing all parameters manually. As for the transfer by name, VB will automatically adjust the order of parameters, the results and the traditional way of transmission is not any different. This means that we can safely use this convenience. And with optional parameters of the process to get C #, the most becomes an optional parameter, also does not cause any other trouble.

PS. Many COM components use the default parameters, and some procedures have a very long list of parameters, in VB can easily handle them, and in C # often allow developers to pass the parameters of vomiting blood


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.