What is the returned value? -- Generic practice: Return Value: Generic practice

Source: Internet
Author: User
Tags date1

What is the returned value? -- Generic practice: Return Value: Generic practice

For a preliminary understanding of generics, see http://blog.csdn.net/lifen0908/article/details/43450921. This blog is mainly about the application of generics in data centers.

1. For example, the returned object:

I use the window for querying basic student information to describe how the returned value is an entity:

The first is layer D:

<Span style = "font-family: KaiTi_GB2312; "> Dim SQL As String 'defines the String variable SQL is used to store the SQL statement to be executed Dim table As able able' defines the table variable table is used to store the execution results and returns Dim paras As SqlParameter () = {New SqlParameter ("@ cardno", studInfo. cardno)} SQL = "select * from student_info where cardno = @ cardno" table = Sqlhelper. sqlHelper. execSelect (SQL, CommandType. text, paras) Dim studentEntity As New Entity. studentinfo If table. rows. count <> 0 Then studentEntity. cash = table. rows (0 ). item ("cash") studentEntity. department = table. rows (0 ). item ("department") studentEntity. grade = table. rows (0 ). item ("grade") studentEntity. sex = table. rows (0 ). item ("sex") studentEntity. studentno = table. rows (0 ). item ("studentno") studentEntity.txt class = table. rows (0 ). item ("class") studentEntity. cash = table. rows (0 ). item ("cash") studentEntity. studentname = table. rows (0 ). item ("studentname") studentEntity. department = table. rows (0 ). item ("department") studentEntity. grade = table. rows (0 ). item ("grade") studentEntity. sex = table. rows (0 ). item ("sex") studentEntity. status = table. rows (0 ). item ("status") studentEntity. ischeck = table. rows (0 ). item ("ischeck") End If </span>
<span style="font-family:KaiTi_GB2312;">Return studentEntity</span>
<span style="font-family:KaiTi_GB2312;">  </span>

As you can see, if our return value is an object, at Layer D, we first need to assign the queried table to the datatable, that is, the table defined here, then assign each field in the table to the object, that is, studentEntity written here.

Look at the U layer:

<Span style = "font-family: KaiTi_GB2312;"> Dim strResult2 As Entity. studentinfo strResult2 = ChaStu. queryCardNO (studInfo) If IsNothing (strResult2) Then MsgBox ("user does not exist") txtcardno. text = "" txtcardno. select () txtcardno. focus () Else txtstudentno. text = strResult2.studentno txtclass. text = strResult2.txtclass txtcash. text = strResult2.cash txtname. text = strResult2.studentname txtdepartment. text = strResult2.department txtgrade. text = strResult2.grade txtsex. text = strResult2.sex txtstatus. text = strResult2.status txtps. text = strResult2.ischeck End If </span>

Layer D's query value is assigned to the object. At the U layer, each object is assigned to the text box on the form, therefore, the function of transmitting the content queried by layer d to the u layer is completed. This is the transfer object, and the return value is also the entity. This is obviously troublesome.

2. Assume that the returned value is datatable.

We will describe the basic data setting form in the data room charging system. The first step of basic data setting is to reflect the basic data setting query result in the database to the form, let's look at the code.

Layer D code:

<span style="font-family:KaiTi_GB2312;"> Dim sql As String        Dim table As DataTable        sql = "select * from BasicData_info"        table = Sqlhelper.SqlHelper.ExecSelectNo(sql, CommandType.Text)        Return table</span>
The operations at Layer D are addition, deletion, modification, and query, so almost all code is repeated.

The code at the U layer directly assigns the table content to the text box, and the code is omitted here.

We can know that returning Datetable is much easier than returning an object.

3. Return to mylist. 1. encapsulate public functions.

Generic is also a table. The query result is given to a table through a function and a query parameter.

First of all, generics, like sqlhelp, are also common functions that need to be encapsulated. because they need to be frequently used in layer D, I create a class in layer D, write down this function. Of course, its restriction is that the attribute name of the object must correspond to the column name.

<Span style = "font-family: KaiTi_GB2312;"> Imports System. collections. generic 'add the Generic namespace Imports System. reflection 'add Reflection in order to use propertyinfoPublic Class ConvertHelper 'to convert a able to a generic set Public Shared Function convertToList (Of T As {New}) (ByVal dt As datatable) as IList (Of T) 'Note: convertToList (Of T As {New}) The new here is used to constrain T and must exist, otherwise, the error Dim myList As new List (Of T) 'will occur when the New T defines the final returned set Dim myTpye 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 For Each dr In dt. rows 'traverses all data Rows of the able Dim myT As New T Dim propertys () As PropertyInfo = myT. getType (). getProperties () Dim pr As PropertyInfo For Each pr In propertys tempName = pr. name If (dt. columns. contains (tempName) Then If (pr. canWrite = False) Then Continue For End If Dim value As Object = dr (tempName) If (value. toString <> DBNull. value. toString () Then pr. setValue (myT, value, Nothing) End If Next myList. add (myT) Next Return myList End FunctionEnd Class </span>

The encapsulated generic has been written. Let's take a look at how to use it.

2. Use public functions-generic (1) Forms

Use all the queried tables to display them in the control. Here we take the query of the collected amount as an example.


(2) entity code

First, because we need a start date and end date, I add two more entities at the Entity layer than the fields in the database, that is, the following two objects are not listed here.

<span style="font-family:KaiTi_GB2312;">  Public Property redate1 As String        Get            Return _date1        End Get        Set(value As String)            _date1 = value        End Set    End Property    Public Property redate2 As String        Get            Return _date2        End Get        Set(value As String)            _date2 = value        End Set    End Property</span>


(3) interface code

Then there is the interface layer. In fact, it is not difficult to write the code, but there are still many errors in the first use.

<span style="font-family:KaiTi_GB2312;"> Function Ichargequ(ByVal LDate As Entity.Rechargeinfo) As List(Of Entity.Rechargeinfo)</span>

(4) D-Layer Code

<Span style = "font-family: KaiTi_GB2312;"> Imports IDALImports EntityImports System. data. sqlClientImports Sqlhelper. sqlHelperPublic Class SqlServerRechargeinfoDAL: Implements Irechargeinfo Public Function Ichargequ (LDate As Rechargeinfo) As List (Of Rechargeinfo) Implements Irechargeinfo. ichargequ Dim SQL As String 'defines the String variable SQL is used to store the SQL statement to be executed Dim table As able' defines the table variable table used to store the execution results and returns Dim mylist As List (Of Entity. rechargeinfo) SQL = "select * from recharge_info where redate >=@ redate1 and redate <= @ redate2" Dim paras As SqlParameter () = {New SqlParameter ("@ redate1", LDate. redate1), New SqlParameter ("@ redate2", LDate. redate2)} table = Sqlhelper. sqlHelper. execSelect (SQL, CommandType. text, paras) If table. rows. count> 0 Then mylist = ConvertHelper. convertToList (Of Entity. rechargeinfo) (table) Return mylist Else Return Nothing End If End Function </span>

(5) U-Layer Code

The code for the factory layer and B layer is not written here. Let's take a look at the code for the U layer:

<Span style = "font-family: KaiTi_GB2312;"> Private Sub button#click (sender As Object, e As EventArgs) handles Button1.Click Dim date1 As Date Dim date2 As Date date1 = DateTimePicker1.Value date2 = DateTimePicker2.Value If DateDiff ("n", CDate (date1), CDate (date2 )) <0 Then MsgBox ("date conflict, Please modify it again", vbOKOnly + vbExclamation, "Modify date") Exit Sub DateTimePicker1.Focus () End If Dim EDate As New Entity. rechargeinfo Dim BDate As New BLL. chargeQuBLL Dim LDater As New List (Of Entity. rechargeinfo) EDate. redate1 = date1 EDate. redate2 = date2 LDater = BDate. chargeQu (EDate) If IsNothing (LDater) = False Then DataGridView1.DataSource = LDater DataGridView1.Columns. remove ("redate1") maid. remove ("redate2") Else date1 = "" date2 = "" End If End Sub </span>

Because the DataGridView control is used, all fields in the Object layer are removed by the datagridview. columns. remove ("redate1") column name.

In this way, all the queried data is displayed on the control DataGridView. That is, the very important datagridview. DataSource = LDater.

(6) display part of the query

But what if we don't want to display all the content and only need one of his data? The syntax is very simple. Here we list the values of the generic type assigned to the Text box, as follows: txttype. Text = mylist (0). type txtname. Text = mylist (0). studentname


Iv. insights

Everything is difficult first and then easy. It is not that difficult to master it, become your own things, and integrate your own understanding.

Rebuild and refuel ~~~~~~~~~~



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.