What is VB.NET (1st, 2)

zhaozj2021-02-17  59

What is VB.NET do (1)

VB.NET enables a lot of C # unable functions, such as When statement, optional parameters, local static variable, object instance access static method, handles binding event, ON Error processing exception, Object direct post-binding, etc. VB and C # are associated with .NET language, compiled is the same CIL, but why VB supports a lot of interesting features. Let's explore it together.

The first is a partial static variable. VB supports a local variable with static keywords, which can keep the variable value at the end of the process:

Public SUB TEST1 ()

Static i as integer

i = 1 'Implement a process call counter

End Sub

We have implemented a simple process counter. Each time TEST is called, the value of the counter increases 1. In fact, there are still many cases we want to keep the variable value. And the Static of the C # cannot be used inside the process. Therefore, to implement a process counter, we must declare a class level variable. This is not as good as VB. Because other procedures cannot be prevented from modifying counter variables. This is a truth with the object package. It should be a local variable of a method. Now I have to be forced to stand independence, obviously a bad design. So how do VB generate local static variables? When the above code is returned, we can clearly see that in the CIL generated in VB, i is not as partial variable, but as a class of field:

. Field Private SpecialName Int32 $ Static $ TEST1 $ 2001 $ I

That is, I is renamed as a field of a class, but is crown in SpecialName. Trying to access $ Static $ TEST1 $ 2001 in your code is impossible because it is not a valid identifier. However, in IL, the code to add this variable is exactly the same as the general class field, which is loaded by LDFLD. I think this method is very smart, turning static variables into the same type field like life cycle, but it is controlled by the compiler to control the permissions of access, so that it becomes a local variable. Also explain why VB uses two different keywords to declare static variables --STATIC and Shared

How is VB.NET to do (2)

VB.NET supports a very interesting feature - MyClass. Most people use MyClass may only be limited to calling this class other constructor. In fact, MyClass can produce some unique usage. MyClass will always call the non-rewritable status of members, that is, when the class method is rewritten, you can use MyClass to get its own version. The following examples are very similar to the examples raised in the VB help.

Public class myclassbase protected overridable subnowding () console.writeline ("Hello form base") End Sub

Public Sub Useme () me.greeting () End Sub

Public Sub UsemyClass () myclass.greeting () End Subend Class

Public class myclassssub inherits myclassbase

Protected Overrides Sub Greeting () Console.writeline ("Hello form Sub") End Subend Class

We use a piece of code to test:

Dim o as myclassbase = new myclassssub ()

O. useme () o.usemyclass ()

The result is the version of the subclass, and the usemyclass has executed version of the base class itself, although this is a virtual method. Viewing its IL, you can see its simple implementation principle: the call command for ME is CallVirt

IL_0001: Callvirt Instance Void App1.MyclassBase :: Greeting ()

And myclass calls Call

IL_0001: Call Instance Void App1.MyclassBase :: Greeting ()

Surprisingly, such a simple feature, I can't implement C # implementation, C # how does I not allow me to call a virtual function in a non-virtual function. C can access its own version of the function with a class name :: method name, but the C # class name can only be used to access static members. This is really C # a strange defect.

转载请注明原文地址:https://www.9cbs.com/read-30577.html

New Post(0)