This series finally walked to the last large language of Visual Basic - generics. In fact, the generic is a special feature supported by .NET Framework 2.0, Visual Basic 2005 only supports him from a language level, just like C # and C / CLI. First, we introduce from the generic itself.
demand
We often have a requirement, that is, the code we have written can be performed for a variety of types. For example, sorting, searching, collection operation, etc. The code for these operations should be able to write only once, it can be widely used for all types. The scheme provided by .NET Framework 1.1 or earlier is to carry any type with the root of the inherited tree - Object. This is equal to compatible with all types of algorithms for Object. However, there are two important issues as all types of bearer containers as all types: First value types are converted to Object, the speed is very slow, so performance is the largest problem. Second, the operation of Object must be performed by runtime type conversion so that the type is not compatible with the type of incompatibility. For example, we have a custom structure - Customer, we want to use ArrayList to save a list of Customer:
Dim Customerlist As New ArrayList () Customerlist.Add (New Customer ("Harry Potter", "HoGwarts School")) "When we want to use this record MSGBox (Customerlist (0), Customer) .name)
It doesn't matter if it looks like this, but if we add the statement to this:
CustomerList.Add ("Hello")
What will it? String Can't convert to a Customer type, but there is no prompt to stop you from doing. This error will be reflected until running. We need a strong type of method to compile type checks and higher performance, so we need generics.
Feel generic
Now let's take a look at generic solutions for the above needs. .NET Framework 2.0 supports a new generic list - List (Of T). Here we introduce the OF statement, he is Visual Basic to increase the generic. Where T is called "type parameters", it accepts any .NET type as the type of element. So we can write the code of the above Customer:
Dim Customerlist As New List (of custom000 Customerlist.Add (New Customer ("Harry Potter", 13, "Hogwarts School") MSGBox (CustomerList (0) .name)
Let's take a look at the places in this code. First, List (Of Customer) is specified, so now the CustomerList object is only a list of Customer type elements. This matter is determined during the compilation period, so the compile period always knows the type of the CustomerList element, so there is no need to perform any type of replacement when removing the object. More importantly, now put the compilation errors in the Customer type, not the original runtime error. The generic can make the compile time to clear the type of operation, which does not perform the value type, so performance is greatly improved. Another advantage of using generics is that Visual Basic's IDE can provide you with intelligent awareness, when you use CustomerList.Add, the prompt containing correct information appears:
Start writing generic code
The generic is provided to you is more than just using the generic type that has been designed in .NET Framework, you can write generic code yourself. Still use of the OF statement: Class MyGeneric (of t)
At this time, the role of the OF statement is no longer providing the type of type required for type parameters, but is defined for new type parameters. Now myGeneric accepts a type of type T. The internal T at MyGeneric generic class is considered a type. You should imagine any types that can be provided after the OF statement when using this generic class. Now just encode T, for example:
Class mygeneric (of t) private myvar as t public sub setvar (byval newvalue as t) Myvar = newvalue end sub public function getvar () as t return myvar end functionend class
Quite simple. When you want to use this generic class, just like the generic class provided by Framework, you want to specify the true type of type T at. Such as this statement:
DIM OBJ AS New MyGENERIC (OF INTEGER)
At this time, the type of OBJ is actually a type of all T in the above definition into Integer. and
DIM OBJ As New MyGeneric (of string)
In this statement, OBJ is an instance of T is a String type. You can arbitrarily specify the type parameters to create more objects suitable for different types, which is the essence of generics - "written once, used for a wide type."
The generics not only can be used for type definitions, but you can also define generic methods. Its syntax and generic type are basically the same:
Public Function IIF (Of T) (transpression as boolean, truepart as t, _ falsepart as t) AS T
This is a generic method, its parameters and return types can use the types represented by the type parameter T. The generic method can use the OF statement like a generic type to determine the type parameter T:
Max = IIF (of integer) (A> B, A, B)
By specifying type parameters, our IIF (OF T) function is a special IIF function for Integer. In fact, Visual Basic also supports the implicit specification of type parameters, that is, when the parameter or return value can have enough information to determine the type parameters, the OF statement does not have to write. such as
Dim Max, A, B AS integra = 100B = 50max = IIF (A> B, A, B)
The types of A and B in the segment code have been identified as Integer IIF (OF T) functions do not have to clearly develop type parameters to know that the type parameters here should be Integer. This will greatly simplify the writing of the code, and the performance benefits brought about by flux.
Next time, I will introduce more usage and points, such as constraints.