VB 2015 Closure (Closure)

Source: Internet
Author: User

Yes, you are right, this article is not about ECMAScript.

Currently VB 14 is more than C # 6 leading features, there is a feature that will be implemented in C # 7, called "Local Method". The difference between this feature and anonymous delegation is that anonymous delegates cannot be stored directly into a variable of the auto-inferred type (in C # as Var) and must be assigned to a variable that already has the specified delegate type, or passed as a parameter. and anonymous delegates are not allowed to be declared as iterators.

Using this feature, you can easily copy the online js/es code to use the delegate, but pay attention to using closures to circumvent some unintended results.

The local method captures the variables of the context just like any other lambda expression or anonymous delegate.

Members inside the local method cannot be accessed externally because the variable has the same name.

If you do not use the Dim keyword and Option Explicit Off, then no declaration variable will be a method-level variable. Note that this is not a global variable.

So, how do you get the variables inside the local method out?

Then you need to define another local method

The result of the execution is a popup message box with a content of 999.

So what's the practical purpose of this notation?

Execution Result: message box content: 999, message box contents: 1000

The F2 in this code takes out the n variables defined in the F1. The delegate nadd modifies the value of the variable n defined in F1.

This notation is valid in the local method, because all variables defined in the local method are stored by the class wrapper with the name similar to _closure$__2-0 and then placed in the variable area of the normal method where it resides.

After the normal method that contains the local method executes, the variable wrapper class such as_closure$__2-0 is detached from the scope, causing the variables in the local method defined in the method to be recycled at any time by GC.

warning : These should not be used in the final release of the product, because late binding can cause performance problems, and not declaring variables will give your code a bunch of warnings (green lines).

Leave a two study questions .

First question: What does the message box say?

Hint: The Green Wave is vs tells you that it might not be the same as you think.

The second question: What does the message box say?

It's not a clue.

VB 2015 Closure (Closure)

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.