vb.net room charge & generic Collection-Implement text box display record

Source: Internet
Author: User

To do the basic data set form, I thought the implementation of this small form should be the simplest! Just click the Modify button to modify it, and then click the Update button to update the data base settings? But when a shot came up with a problem, how does the data in the datasheet appear in the text box? This really suddenly baffled myself! But there is always a solution to the problem.

Now that the entity is returned to a text box, it should be possible to call the entity layer directly. Does this make it perfect? However, the biggest drawback is that the next encounter of the control and data call form of the same, so that does not repeat the code to achieve the repetition? It's a serious violation. In order to extract the duplicate parts, convenient for subsequent calls, so the generic set is used.

So first of all, what is the use of generic collections? In the basic data setting, the function we want to implement is to convert the information in the data table into a single entity and then add it to the text box, so we can pass one of the entities in the data table as a parameter to the generic type and get what we want.

Because list is a generic equivalent class of ArrayList, it belongs to the dynamic array, which can satisfy its own needs. We can talk about type as a template, so that the variant part of the template can be replaced by the name of the class that is coming in, thus getting a new definition.

Prior to this, it was necessary to establish the entity layer, because the basis for creating the collection was to have a class before the object could be placed in the collection.

Established Entitymodel:

Description of ' ******************************** ': generic collection, Complete conversion of DataTable type to entity class ' Hoya ' group: XX ' Date created: 2014-07-20 22:28:21 ' version number: V1.00 ' * * * * Imports System.Collections.Generic ' Adds a generic namespace for managing generic sets of imports System.Reflection ' add reflection, In order to refer to propertyinfopublic Class entityhelper ' Converting a DataTable to a generic collection public Shared Function convertolist (of T as {New}) (Byv Al DT as DataTable) as IList (Of T) Dim myList As New List (Of T) ' defines the final returned collection Dim myType as Type = GetType (t ) ' Get the type name of the entity class Dim dr As DataRow ' define rowset Dim tmpname as String = String.Empty ' Define a temporary variable ' traverse datatab All data rows of Le for each Dr in Dt. Rows Dim MyT As New T ' defines an object for an entity class Dim Propertys () as PropertyInfo = Myt.gettype ().                GetProperties () ' Defines the property collection Dim PR as PropertyInfo ' to traverse all properties of the object for every PR in propertys Tmpname = pr. Name ' assigns the property name to the global variable ' to check if the DataTable contains this column, (the column name = = Property name of the object) if (dt. Columns.contains (Tmpname)Then ' compare this property to the column name of the DataTable Lee to see if the DataTable contains this property ' to determine if this property has a setter (class) if (pr.                    CanWrite = False) Then ' to determine if this property is writable and if not writable, jump out of this loop Continue for End if Dim Value As Object = DR (Tmpname) ' Defines an object-type column that holds the values of the columns If (value. ToString <> DBNull.Value.ToString () Then ' determines whether it is empty, or if not NULL, assigns the property PR to the object.            SetValue (MyT, value, nothing) ' dynamically accesses a dynamic property through reflection during runtime End If End if Next Mylist.add (MyT) ' Add to collection Next return myList ' return entity collection End FunctionEnd Class
so that the D layer can be called directly to convert a set of t_datatable into a generic collection
Public Function Readbasic () as IList (of entity.basicdataentity) Implements Idal. Ibasicdatadal.readbasic    ' should use generic collection type        ' Dim sqlparams as SqlParameter () = {}  ' declare and instantiate parameter array        ' call no parameter query ()        ' return clssqlhelper.query (strSQL, CommandType.Text)    ' calls SqlHelper        ' return table             ' to return query results        ' Dim mysqlhelper As Ne ' W sqlhelper.sqlhelper   ' defines the query helper class, already defined in        the Dim dt As New DataTable   ' definition query to the table set        Dim MyList as List (of entity.basicdataentity)    ' save converted generic collection        Dim strSQL as String = "SELECT * from T_basicdata"  ' Declare and instantiate the SQL statement that needs to be executed        dt = Clssqlhelper.query (strSQL, CommandType.Text)   ' Execute query        ' converts DT to generic collection        MyList = Entityhelper.convertolist (of Basicdataentity) (DT) return        myList    ' returns an entity    End Function
In fact, the whole process is just called a generic collection, changed its type, and the use of the DataTable is the same process.

Public Function Readbasic () as IList (of entity.basicdataentity)        ' declares and instantiates a variable Inbasic: Calls Factory returned Ibasicdatadal        Dim inbasic as Ibasicdatadal = Factory. Basicdatadal () ' declares and instantiates     factory for DataAccess class ' in advance to declare the type of factory, declares and instantiates the        variable readb        Dim readb as IList (of entity.basicdataentity)        readb = Inbasic.readbasic () return        readb     ' returns readb this generic array    End Function

This allows you to call its array directly in the U layer and then display it in the text box.

Dim readb As IList (of basicdataentity)      ' defines a generic collection        Dim rbasic as New basicdatabll           ' defines a B-layer interface        READB = Rbasic.readbasic ()        txtrate.text = readb (0). Rate        Txttmprate.text = readb (0). Tmprate        txtlimitcash.text = readb (0). Limitcash        txtleasttime.text = readb (0). Leasttime        Txtpreparetime.text = readb (0). preparetime        txtunittime.text = readb (0). unittime
Note:

In fact, after all, suddenly feel t_datatable in just a data, if just to query a data, perhaps directly call the entity layer will be more simple, such as landing form, of course, can also call the generic collection directly, but also just want to query the corresponding user name and password, That's all! If it's just a form, it really feels a bit overqualified. It is like a big water tank, you have to put a liter in it, it is not superfluous it? You might as well just take a small cup of water and put it more directly.

In this use of generic collections, one is to be familiar with, moreover, the whole system needs to use a generic set of places there are many, here also just call a public part, reduce the duplication of code, and why not?


vb.net room charge & generic Collection-Implement text box display record

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.