What is VB.NET? (3) - Handles and WitHhevents
In addition to the method response in addition to the C #, VB can process the unique event processing from VB5 inherited --withevents.
I like to call this event processing method for static event processing, when writing a response event, which is determined which event responds, and C # is binding events in the code. For example, this simplest example:
Public Class Handlerclass Public WitHevents Myobj As EventClass
Private sub myobj_myevent (byval e as system.eventargs) Handles myobj.myevent msgbox ("hello") End Sub
Public Sub New () Myobj = New EventClass End Subend Class
EventClass used in the code is like this:
Public Class EventClass Public Event MyEvent As EventHandler
Protected Overridable Sub OnmyEvent (Byval E As Eventargs) RaiseEvent MyEvent (ME, E) End Sub
Public Sub Test () OnMyevent (New Eventargs) End Subend Class
Let's review, this code is implicitly written two methods - ADD_MYVENT (EventHandler) and REMOVE_MYEVENT (EventHandler), which actually uses these two methods to bind events by calling these two methods. Release the binding. C # also allows you to write your own event bind / release the binding code.
So how is WitHevents work? VB.NET compiler will automatically
Public WitHevents Myobj As EventClass
Translated into the following process:
Private_myobj as EventClass
Public property myobj () As EventClass Get Return_MYOBJ End Get Set (Byval Value As EventClass)
IF not (me._myobj is nothing) Then RemoveHandler _myobj.myevent, new eventhandler (addressof myobj_myevent) Endiff
ME._MYOBJ = Value
IF me._myobj is nothing kilion
AddHandler_myobj.myevent, New EventHandler (addressof myobj_myevent)
End Set End Property
This shows that this property is automatically triggered when the WitHevents variable is assigned to bind events. Most of the events we have use are 1 pair 1, that is, one process responds to an event, so this WitHevents static method is very useful, it can significantly enhance code readability, but also make events in VB.NET It is very convenient to handle, unlike C #, you must have a manual binding event as you leave the form designer.
However, when analyzing this IL, I also found VB.NET in the translation of small problems, which is too much ldarg.0, which is frequently using Me or This performance, so we must be in the encoding process. It should be noted that in addition to using the Me / this itself reference, do not bring me / this when using its members, such as Me.Myint = 1, change to myint = 1, such a small habit will bring you big Performance benefits. How does VB.NET do (4) - Type conversion
Today, I saw a post at www.aspx.cn, I saw that Wyhw Heroes really hoped DOTNET Blog a .NET Boutique Technology Blog. So I can't be lazy, learn quickly, and have multiple technical posts. Go back to the topic, I saw a new operator in Visual Basic 2005 today, I will join a new operator in Visual Basic 2005, which is equivalent to the C #'s AS operator. I have always hope that VB has such an operator. Since I saw it today, I will study the type conversion of VB and C #. First, VB, type conversion operators are mainly CType and Directcast. Their usage is almost the same. I have compared these two operators in detail, I got the following conclusions:
1. When converting into a reference type, there is no difference in calling CastClass instructions, unless the type conversion operator is overloaded (of course, VB.NET is not yet, but I don't have VB 2005, so I can't test it).
2. When converting into a value type, CType calls VB specified type conversion function (if any), when converting String to INT32, will automatically call VisualBasic.comPilerServices.integertype.FromString, and convert Object to int32 Will call fromObject. When other numerical types are converted to int32, the CType also calls the type of conversion method to implement conversion. The Directcast operator is very simple and the object is directly removed into the required type.
So when used in value type, CType does not have DirectCast fast but can support more conversions. In C #, type conversion is (Type) operator and AS operator. (TYPE) Operator works similar to VB's DirectCast, but also directly unpacking or castclass, but if the supported type conversion (such as long to int), (type) operator will also call the corresponding conversion method But do not support the conversion from String to INT. C # another operator AS is more intelligent, as long as it determines whether the object's run instance can be converted to the target type, then the CastClass instruction can be omitted, and the compiler can also optimize the AS directly. Such as saving an object reference. So when converting Object to the desired type, AS is the best choice.
Since AS has a lot of advantages, Visual Basic 2005 absorbs this feature, and uses the TRYCAST operator to get the same effect as the AS, and the syntax is the same as Directcast or CType.