About dynamic identifiers for visual Basic 9.0

Source: Internet
Author: User
Tags reflection visual studio

Dynamic languages have been developing very quickly this year, with a variety of scripting languages with a number of dynamic language features. Dynamic language has won the favor of many people with its flexible syntax and rich run-time behavior. In the. NET Camp, C # has been playing the role of static language, although 3.0 of the syntax changes are radical, but still focus on compile-time type checking and constraints. The use of the dynamic characteristics of the bad will produce more run-time problems, not easy to ensure the reliability of the program, but its role in improving development efficiency is beyond doubt. Visual Basic 9.0 is the only language in Visual Studio that allows you to dominate both dynamic and static areas, allowing you to freely choose the style you like in your program.

Visual Basic supports late binding from 1.0, that is, the object is used according to the Run-time type information of the object, not the type information when the object is defined. Such as:

Dim L as Object = New ArrayList () L.add () l.removeat (0)

We know that object does not have an Add or RemoveAt method, but the Run-time type ArrayList have them, so Visual Basic allows the runtime to double-check for actions and support. Now that this process has been done at runtime, we should allow the late-bound operation itself to be determined at run time as well. This syntax enhancement is called a dynamic identifier. For example, we can write this:

Dim L as Object = New ArrayList () L. ("Add") L. ("RemoveAt") (0)

We see that this time the method name add and RemoveAt is already a string, which of course can also be a variable containing a string! So you can even write code like this:

L. (Console.ReadLine ()) (a) L. ("Remove" & "at") (0)

In this way, the identifier as the method name can be determined entirely during the dynamic period. The method name is so, what about the type name? It should be so. We can write this:

Dim L as Object = New ("ArrayList") ()

In this way, the type of your variable l will be determined based on the value of a run-time string. Dynamic identifiers also animate the parameters of a function, assuming that the following subroutines are available:

Sub Method1 (Optional arg1 As String, Optional arg2 as String)

When running, we can dynamically specify which parameter to assign the value to:

If a > 0 Then argstring = "Arg1" Else argstring = "Arg2" Method1 ({argstring}:= a.tostring ())

Note the curly braces before the: = Symbol, which represents a dynamic combination of parameter names. With the feature of dynamic identifiers, Visual Basic 9.0 does not need to take advantage of the complex syntax of reflection to do many tasks that require reflection and run-time type information, making Visual Basic the best helper to solve your little problems at hand. In this issue we have a taste of the dynamic features of Visual Basic 9.0, and the next phase will introduce the second dynamic feature-the dynamic interface.

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.