Use of generic Sets

Source: Internet
Author: User

The restructuring of the data room charging system is nothing more than adding, deleting, modifying, and querying four database operations. In addition, we use a three-tier architecture for reconstruction, and layer D is used to deal with databases. To perform these four operations, we need to return values. addition, deletion, and modification are not mentioned here, but when we perform queries, layer D needs data to be sent to layer B, so what form should we return the best?

When I first came into contact with Layer 3, I returned a datatable, DT. rows [0] ["XXX"] is very powerful when it is realized. Later, I communicated with you and found that there is actually a better way to use it, is to use the object class to fill the generic set.

The problem arises:

1. What is a generic set?
2. Why is it better to use it? 1. What is it?

Generic, as the name implies, is a generic type. That is, there is no definite type. How to Use it if there is no definite type? In fact, you can define the type when using it.

A set is a class that processes multiple data types. Generally, you can use different forms of the same set in multiple applications. Instead of creating a set based on the draft, you need to create a prototype using the generic type ).

In use, replace T in list <t> Angle brackets with the corresponding type based on the data type to be processed, and create the corresponding instance.

2. Why?

First, you need to add the following imports statement to the Code:

Imports system. Collections. Generic


After the imports statement is added, you can create a generic class. Basic classes look very common. You can use attributes, functions, subprograms, fields, or anything else in the class.

Public class converthelperentity public shared function converttolist (of T as {New}) (byval dT as datatable) as ilist (of T) 'convert able to generic set dim mylist as new list (of T) 'to define the final returned set dim mytype as type = GetType (t) 'Get object class type name dim Dr as datarow' defines the row set dim tempname as string = string. empty 'defines a temporary variable 'to traverse all data rows in the datatable for each DR in DT. rows dim MYT as new t' defines the object dim propertys () as propertyinfo = MYT of an object class. getType. getproperties () 'define the property set dim PR as propertyinfo' to traverse all properties of this object for each PR in propertys tempname = Pr. the name' assigns the property name to the Temporary Variable 'and checks whether the datatable contains this column (column name = object property name) if (DT. columns. contains (tempname) then 'compares this attribute with the column name in the datatable to check whether the datatable contains this attribute' to determine whether the property has a setter if (PR. canwrite = false) then' determines whether this attribute can be written. If it cannot be written, the loop continue for end if Dim value as object = DR (tempname) appears) 'define an object-type variable to save the column value if (value. tostring <> dbnull. value. tostring () then' if it is not null, the property PR is assigned to the object. setvalue (MYT, value, nothing) 'dynamically accesses the attribute end if next mylist of an object through reflection during runtime. add (MYT) 'add to the set next return mylist' returns the object set end functionend class
We need to add an object class in the object Layer, because the basis for creating a set is to have a class before we can put the object into the set.

So why do we say it is better than datatable? With a generic set, we convert datatable into a single object class at Layer D, and then fill the object class in the generic set.
Its core concept diagram:


Comparison of its advantages and disadvantages can be found:

Datatable

List <t>

It is very easy to write errors, and the compiler does not check

Click it and click it.

Must understand the database structure

No need to understand the database structure

Not in line with Object-oriented Thinking

In line with Object-oriented Thinking

The data type of the field cannot be intuitively seen because the datatable is weak.

The attributes of an object class are strongly typed. The type of each field is known.

By learning generic sets, you can really understand that a program may not be the most important to implement functions. How to Make the Code the simplest, easier to expand, and how to think for others the most, this is what we are going to do.

Use of generic Sets

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.