Introduce generics to Visual Basic programmers (1)

zhaozj2021-02-11  241

This document is

Sean Campbell

,

Scott Swigart

"Kris Horrocks, Derek Hatchard, And Peter Bernhardt." To Microsoft Visual Basic Programmers Introduction Whidbey "book, copyright belongs to the original author and publishing house. Reprinted please indicate this statement.

This article uses popular terms and a large number of examples to introduce the new features of the next version of Visual Basic.NET to the Visual Basic .NET. This article can help vb users understand generics so that generics will be applied to their own applications.

Applications: generics

This app showcases new generic features in Visual Basic.NET.

new concept

It is necessary to take a point to analyze why this feature is to be taken before starting to achieve generics. Generic technology derived from a general method to handle various possible types of objects without having to care about their specific type. For example, in Visual Basic 6.0, you can store any types of objects with a Collection class.

Visual Basic 6.0 collection

Visual Basic 6.0 does allow you to store anything in a collection. However, there are several restrictions in the Collection class. We use an example to explain how to store this Employee class in a collection:

'Visual Basic 6.0 Code: Class Module EMPLOYEE

Public SSN As String

Public firstname as string, PUBLIC FIRSTNAME

Public Lastname As String

Public Salary As Currency

The method stored in the collection is very straightforward.

'Visual Basic 6.0 Code

DIM Employees As New Collection

DIM EMP As Employee

SET EMP = New Employee

Emp.ssn = "111-11-1111"

Emp.firstname = "scott"

Emp.lastname = "swigart"

Emp.salary = 50000

Employees.add EMP, EMP.ssn

This code first created an instance Employees for a Collection. The Employee class then created an instance and set some data. Finally, the Employee object is added to the collection, specifying the EMP.ssn property as a keyword. The following code shows how to remove this Employee object from Collection:

'Visual Basic 6.0 Code

DIM EMP2 AS EMPLOYEEEE

SET EMP2 = Employees ("111-11-1111")

Now let's study what restrictions on this collection of Visual Basic 6.0. First, your original intent is to let Employees only store the object of the Employee type. But there is no mechanism to prevent one other object from placing in this Employees collection, and there is no thing that can tell you what type from this collection. The following code can compile correctly:

DIM S As String

s = Employees ("111-11-1111")

Although developers can clearly know that this cannot work correctly, there is no way to let the compiler discover this problem. This will happen a runtime error. The use of the collection also limits the play of intelligent awareness technology. Take a look at the code below:

Employees ("111-11-1111"). LastName = "someoneelse"

This shows that you can directly edit the items in the collection. However, IDE's smart sensation cannot help you choose a lastname property. Returning again, the previous Visual Basic's collection can be stored anything. Two largest restrictions using a collection are losses in performance and flexibility. Although the collection is easy to use, the performance is very poor as a dynamic array. The collection design makes it more like a dictionary, so when you need the data structure similar to a stack or queue, it is not a good choice.

Collection in the framework

.NET Framework 1.0 / 1.1 Some problems are solved by adding the species of the collection. After the newly introduced System.Collections Namespace, you can create more types of collection, such as array tables, bits, hash tables, queues, sort tables, and stacks. The following table lists these types of usage:

Collection name

use

ArrayList

An array table can create a dynamic increasing array.

BitArray

The bit array is an array of optimized, dedicated to the storage of Boolean (true / fake).

Hashtable

The Hash Table and the Collection class in Visual Basic 6.0 is very similar. It allows you to find the corresponding value by keywords. However, keywords and values ​​are still arbitrary.

Sortedlist

The sort table and the hash table are very similar, the only difference is that its keyword is always sorted. This means that when you use for ... EACH syntax to traverse the entire collection, the items are always sorted.

Queue

Queue is a collection of advanced first out of the object that is stored.

Stack

The stack is a collection of first out of the object that is stored.

.NET Framework 1.0 / 1.1 has solved the collection of the collection in Visual Basic 6.0 in flexibility, but these collections are still weak type, so you can still put anything in an arraylist, although in a designated In applications, only the only type of storage is meaningful.

You really want to specify that each keyword must be String and each value is an Employee type. In the .NET framework 1.0 and 1.1, you only create your own class, of course, this method is somewhat cumbersome. If you use a new .NET framework 1.2, this problem can be solved with a very little code, the method is to use generics.

Go deep into code

The generic type can provide strict type inspection, better intelligent perception and better performance, as described in the above section. In other words, they surpass the advantages of all previous collection classes.

Feel generic

Next, you will see that when you create an instance of a generic collection, you need to provide some information so that the set class can be strongly managed. This has a lot of advantages, including more checks in compilation phase to ensure a more secure and more reliable code, better intelligence, and better performance. It is necessary to mention that .NET Framework 1.2 is newly increasing a generic collection on the basis of previous versions. .NET frame 1.2 will not force you to use generics.

If you want to use generic types, you first need to include System.Collections.Generic this namespace. This allows access to Dictionary, List, Queue, SortedDictionary, and Stack classes with generic feature. The code in the BTNConsumeGenerics_Click event provides an example of using a generic dictionary:

'Visual Basic .NET 8.0 Code

Private sub btnconsumeGenerneics_click (Byval E AS System.EventArgs) Handles BtnconsumeGenerics.click

DIM Employees As New Dictionary (of string, employee)

DIM EMP As Employee

EMP = New Employee

Emp.ssn = "111-11-1111"

Emp.firstname = "scott" Emp.lastname = "swigart"

Emp.salary = 50000

Employees.Add (Emp.ssn, EMP)

DIM EMP2 AS EMPLOYEEEE

EMP2 = EMPLOYEES.ITEM ("111-11-1111")

DIM S As String

's = Employees.Item ("111-11-1111")' this is now a syntax error

Employees.Item ("111-11-1111"). LastName = "someoneElse"

End Sub

In-depth view this code, you will notice quite interesting things in some generic technology. The first type of generic is embodied in this way:

DIM Employees As New Dictionary (of string, employee)

This can be translated into "Creating a Dictionary, its keyword is the string type, the value is Employee Type." Trying to store an object that is not an Employee type will cause compilation errors. It is necessary to reiterate that if a generic is used, you will use the wrong type to be compiled and not runtime errors. In fact, the following code unless commented, it will not be compiled, just like the compiler knows Dictionary is dedicated to save the Employee object, not String:

's = Employees.Item ("111-11-1111")' this is now a syntax error

Further, you can now get a comprehensive perceptual support. If you enter "Employees.Item (" 111-11-1111 ").", Will automatically pop up member of the Employee type, this shows that Visual Studio knows that Dictionary is now specifically stored in the Employee class.

(Not finished, pick "to the Visual Basic programmers to introduce generics (2)")

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

New Post(0)