Vb. NET is how to do (five or six)

Source: Internet
Author: User
Tags integer


vb.net how to do It (v)--Implement Interface

VB.net's implementation of the interface syntax is the VB5 invention of the implements, the implementation of interface syntax in today's mainstream language is unique. For example, I have two interfaces:

Interface Interface1
Sub Test ()
End Interface

Interface Interface2
Sub Test ()
End Interface

The two interfaces have exactly the same member test. What if I need to implement two interfaces with a class at the same time? First think about it, if it is java,jscrip.net such a language can only use a test function to implement the two interface test members. If two test is only accidental duplicate name, its content must be implemented separately, so some solution interface duplicate design appeared ... In VB, the unique implements statement allows you to think how to implement the interface how to implement, such as the following class implementation with two names are not the same way to implement two interfaces.

Public Class Implementation
Implements Interface1, Interface2

Public Sub Hello () Implements interface1.test

End Sub

Private Sub Hi () Implements interface2.test

End Sub
End Class

In other words, VB allows the function of any name to implement the members of the interface, and the accessor can be arbitrary, such as to use public or private can.

C # provides an explicit implementation (explicit implementation) syntax for dealing with duplicate members, which implements the syntax of the two interfaces

public class Class1:interface1, Interface2
{
Public Class1 ()
{
}
void Interface1.test ()
{
}

void Interface2.test ()
{
}

}

Note here, C # can only use the interface name. Name of the member name to name the implementation method, and the accessor can only be private and cannot expose a method that is explicitly implemented.

After examining the IL, I found that. NET supports both implicit and explicit implementations. The implicit implementation is simply to put a method in the class with the same name as the interface member method--This kind of VB does not support. An explicit implementation is added to the descriptive information of the method:

. Override Testapp.interface1::test

Whether it is the explicit implementation of C # or VB Implements statements are such a principle. Other words. NET provides the function of exchanging name to implement interface member, but only VB has given this freedom to the user, but other languages still adopt the classic grammar.


Vb. How NET is done (vi)--default properties and property parameters


In the original VB6, there was a peculiar function-the default attribute. In VB6, the name of an object can directly represent the default property of that object. For example, the default property of a TextBox is text, so the following code

Text1.Text = "Hello"

can be simplified to

Text1 = "Hello"

This simplification brings a lot of trouble to VB, assignment operation requires two keywords--let and set, the result attribute process also need let and set two kinds. And this feature can still work when it is late bound. To vb.net, this feature is greatly limited, and now only attributes with parameters can be used as default properties. Such as

List1.item (0) = "Hello"

can be simplified to

List1 (0) = "Hello"

This syntax makes an object with a default property look like an array. So how does VB judge whether a property is a default property? Look at the following code

Public Class Proptest

Public Property P1 (ByVal index as Integer) as String

Get


End Get

Set (ByVal Value as String)


End Set

End Property


Default Public Property P2 (ByVal index as Integer) as String

Get


End Get

Set (ByVal Value as String)


End Set

End Property

End Class

The P1 and P2 two properties are essentially identical, and the only difference is that P2 has a default modifier. After disassembly of this class, two attributes are found to be identical, without any difference. However, the Proptest class is added with a custom meta attribute System.Reflection.DefaultMemberAttribute. The member specified by this meta attribute is the default type used by InvokeMember, which means that the default property can also be used for late binding. But I experimented. Adding the DefaultMember meta attribute manually to a type does not achieve the ability to make a property default. It seems that this function is a VB "grammatical sweetness." However, VB or C # compiler for other generated classes of the default properties should only be judged by defaultmemberattribute, so I will be a VB class only DefaultMemberAttribute Specify a default method, do not use default, Then compile it for C #, and sure enough, C # will recognize it as an indexer (indexer)!

Now that we've talked about the C # Indexer, we'll take a look at the differences between VB and C # properties. The result of the experiment just now is that the default property of VB is the indexer in C #. However, VB can still use the syntax of the property to access the default properties, and C # can only use the syntax of the array to access the indexer. More specifically, VB can create a property that is not the default but the attributes with parameters, such as the P1 in the example above, and C # does not support attributes with parameters, if the VB written, with the parameter attributes of the class to C #, C # will prompt the "property is not supported by this language, please use GET_XXX and set_ Syntax access for XXX. " That is, a property with parameters is a feature of the CLR, but not CLS-compliant (Common language specification), and thus a cross-language barrier occurs. This also reinforces our understanding of the CLS--if you want your code to work across languages, be sure to be aware of CLS-compliant.



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.