Complex and efficient - Visual Basic.NET new power

zhaozj2021-02-11  215

Visual Basic is Microsoft's old language, developing to Visual Basic.net has experienced huge changes. Now Visual Basic.net is the "most efficient" language identified by Microsoft. Indeaal Basic.net maintains an easy-to-understand style, plus no one in IDE is more syntax correction and strongest intelligent sensation, it can indeed refer to the development of the .NET. Microsoft defines Visual Basic.Net as an efficient language, and does not want it to be too complex, and can always be efficiently used by users to be its purpose, but also because of the lack of some high-level usage and C # and other languages. In the upcoming Visual Studio.Net Whidbey, Visual Basic has changed the previous style, a large amount of more in-depth features joined Visual Basic.net, such as generics, operator overload, etc., is Microsoft to make Visual Basic become a complex Profic language? However, after browsing these features, I got a conclusion, that is, after Visual Basic, after these advanced language characteristics, I still maintain a simple feature of simple and rapid development, it seems that this upgrade will give an old Visual Basic. Bring new vitality.

First, OF statements and generics

The new .NET Framework will have generic feature, which is everyone know. So I don't have a generic worry at all, if VB doesn't have a generic, how to use the generic code created by C #? VB provides an OF statement on the processing template:

Public Class TestGeneric (of t)

Private mitem as t

Public Function GetItem () AS T

Return me.mitem

END FUNCTION

END CLASS

Here is a template T with an OF statement, which is an undecided type. When you use, we can specify a specific type with the OF statement, such as:

DIM OBJ AS TestGeneric (of INT32)

DIM I as INT32 = Obj.getItem ()

The OF statement here specifies the type System.Int32 to the template, so TestGeneric's type T completely turns integers. This is made automatically by the compiler. In addition to types, templates can be used, and methods can also be used, such as:

Public SUB SWAP (Of T) (byref a as t, byref b as t)

You can also use the same syntax when you use:

DIM X, Y as string

X = "a": y = "b"

Swap (of string) (x, y)

Just as the SWAP subroutine is defined as a String type from the beginning! With a generic, we can create code that is efficient and highly reused, and its strong type features provide convenience for input code - intelligent perception can determine the true type of the template in time to provide code automatic completion.

Second, operator overload

To support CLS, Visual Basic.NET does not support the definition of operator overload, nor allowing other language overloaded operators. If C # can be easily performed:

Uint

32 a

, B;

BOOL F = a == b;

And the complete peerful VB.NET statement is written:

DIM A, B AS uint32

DIM f as boolean = uint32.op_equality (a, b)

Although operator overload does not meet CLS, even the opportunity to use is too cruel to the VB programmer. Now the WHIDBEY version of Visual Basic.net can not only use the overloaded operator, but also overload the operator. Overloaded operators include one yuan operator , -, NOT, ISTRUE, ISFALSE, WIDENING, Narrowing; binary operator , -, *, /, /, l,,, , or, ^, <<, >>, =, <, <>, <,> =, <=, etc. The syntax when overloading is similar to the subprograms and functions. It is an Operator statement, such as achieving a plurality of addition: Shared Operator (Byval C1 As Complex, Byval C2 As Complex) AS Complex

Return New Complex (C1.Real C2.Real, C1.Imaginary C2.Imaginary)

End Operator

Using the operator overload can make the program simply, you can now use a clear operator to replace the original multi-nested function called.

Third, no symbol integer and sbyte

No symbolic integer, including UINTEGER, USHORT, and ULONG, plus SBYTE with symbols, is now supported by Visual Basic.net, uinteger is 32-bit unsigned integer, the literal symbol is UI; Ushort is 16-bit no symbol Integer, the literal symbol is US; ulong is an unsigned 64-bit integer, the literal symbol is UL; Sbyte is 8-bit symbolic integer. With no symbol integer, Visual Basic can make it easier to create type derived from Iconvertible, without having to worry about these types of these types in VB. At the same time VB can also use these types to complete more operations, which is important in graphical programming.

Visual Basic.Net also provides an operator that directly converts unsigned types, they are Cuint, Culong and Cushort. You can use unsigned integers like this:

DIM UI as uinteger = 123ui

DIM UL AS ULONG = 2147483648UL

Dim US as ushort = Cushort (UI)

Fourth, Continue statement

VB has a powerful circulating statement, its DO loop, for loops, and while loops are widely used by programmers. However, VB's cycle statement lacks the function of jumping into the next round of loops, sometimes we have to use a goto statement, such as

For i as ingeger = 1 to 100

IF i mod 2 = 0 THEN GOTO _NEXT

Odds.Add (i)

_Next:

NEXT

Now, Visual Basic.net can easily jump into the next round of loops using the Continue statement:

For i as ingeger = 1 to 100

IF i mod 2 = 0 Then Continue for

Odds.Add (i)

NEXT

And the Continue statement can also pick up for, do or while, which is convenient for us to use cyclic nested. A very early programmer hopes that VB has a statement like Continue, and now Whidbey has finally realized our desire.

Five, "My Name Space" - MY Namespace

My namespace is a fairly thoughtful design in Visual Basic.net, and my namespace is like a namespace created with the Namespace statement, but is actually a virtual namespace containing a lot of body tools. The MY namespace actually points to the various corners of the .NET Framework class library, providing you with the most commonly used system or working environment. It mainly includes: My.Application provides the current program running environment, such as the title, version number, running directory, etc. of the assembly.

My.computer provides information on platforms and hardware such as access registry, printer, keyboard, screen, etc.

My.user provides information such as the current user display, user domain.

My.WebServices provides information about WebService that you have referenced, allowing you to quickly access WebService.

My.Forms provides all forms in the current engineering, you can quickly create their instances here.

Look! What more convenient features, just enter my, "My Name Space" is now displayed. This is the monk of Visual Basic.Net, and users in other languages ​​can not be able to enjoy.

6. Using block

People who have used C # are familiar with the tool for the DISPOSE method used to automatically call the object. Now Visual Basic.net also has this feature, as long as the object is defined in the USING block, the object will be automatically called after the object is used to release the memory, and of course the object must implement the IDisposable interface. Such as:

Using Songfont AS New Font ("Song",

12F

)

TXTFONT.TEXT = SongFont

END Using

I have seen the new features of Visual Basic.Net. Are you thinking that Visual Basic.Net has these powerful features, it maintains its original convenience, easy to understand and efficient? It seems that Microsoft's careful design has indeed turning Visual Basic into a "complex and efficient" language.

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

New Post(0)