vb.net how to do it (four or five)

Source: Internet
Author: User
vb.net how to do It (iii)--handles and WithEvents

VB in addition to using C # as a way to deal with the event response, there are inherited from the VB5 unique event handling way--withevents.

I like to call this event handling as static event handling, when writing a method of responding to an event, you decide which event the method responds to, and C # binds the event in code. For example, here's the simplest example:

Public Class Handlerclass
Public WithEvents MyObj as EventClass


Private Sub myobj_myevent (ByVal sender as Object, ByVal e as System.EventArgs) Handles myobj.myevent
MsgBox ("Hello")
End Sub

Public Sub New ()
myobj = New EventClass
End Sub
End Class

The EventClass used in the code is 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 Sub
End Class

Let's review the code implicitly to write two methods--add_myevent (EventHandler) and Remove_myevent (EventHandler) to EventClass, In fact, any context that uses the event is invoked to bind the event and unbind the two methods. C # also allows you to write your own event binding/unbind code.

So how does WithEvents work? The vb.net compiler automatically compiles the

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 are nothing) Then
RemoveHandler _myobj.myevent, New EventHandler (AddressOf myobj_myevent)
End If

Me._myobj = Value

If Me._myobj is no Then Exit property

AddHandler _myobj.myevent, New EventHandler (AddressOf myobj_myevent)

End Set
End Property

Thus, when assigning a value to a WithEvents variable, this property is automatically triggered to bind the event. Most of the event responses we use are 1 to 1, a process that responds to an event, so this WithEvents static method is very useful, it can significantly enhance the readability of the code, but also make the event handling in vb.net very convenient, unlike C # That leaves the form designer to bind events manually.

But in the analysis of this IL, I also found vb.net in the translation of a small problem, that is, ldarg.0 appear too much, this is the frequent use of me or this performance, so we must pay attention in the coding process, in addition to the use of Me/this itself reference, Use its members do not bring me/this, such as Me.myInt = 1 change to MyInt = 1, such a small habit will bring you a lot of performance benefits.


vb.net how to do It (iv.)--type conversion


Today in www.aspx.cn saw a post, see Wyhw really hope dotnet blog become. NET's boutique technology blog. So I can not be lazy, and quickly learn, and many technical posts.
Back to the point, I saw today in Visual Basic 2005 adding a new operator--trycast, equivalent to C # 's as operator. I have always hoped that VB has such an operator, since today, I saw the way to study VB and C # type conversion. First of all, VB, type conversion operators are mainly CType and DirectCast. Their usage is almost the same. I compared the two operators in detail and came to the following conclusions:

1, when converting to a reference type, the two are not any different, are directly called castclass instructions, unless the type conversion operator overloaded (of course, vb.net not yet, but I do not have VB 2005, so can not be tested).

2. When converting to a value type, CType invokes the type conversion function specified by VB (if any), such as converting a string to Int32, The VisualBasic.CompilerServices.IntegerType.FromString is invoked automatically, and Fromobject is invoked when object is converted to Int32. When other numeric types are converted to Int32, CType also invokes the conversion method of the type itself. The DirectCast operator is simple enough to simply disassemble the object into the desired type.

So when it comes to value types, CType does not directcast fast but can support more conversions. In C #, type conversions are (type) operators and as operators. The (type) operator works much like VB's DirectCast, and is also a direct unboxing or castclass, but if you encounter supported type conversions (such as long to int), the (type) operator also invokes the corresponding conversion method. However, conversions from string to int are not supported. C # Another operator as is more intelligent, it is only to determine whether the object's running instance can be converted to the target type, then you can omit the castclass instruction, directly to the known type of operation, and the compiler can also automatically optimize as, such as saving an object reference. So when you convert object to the desired type, as is the best choice.

Because as has many 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.




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.