TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to VBNET

VB.NET DataRow Field Extension

Use the Field extension on the DataRow type. Use a type argument to get fields.
DataRow Field. The DataRow has a Field Function. This provides support for getting a field with no casting required. The field value is returned in the object type specified. It is a generic Function. We use it with special syntax.DataRow
Example. This program first creates a DataTable. The table has four columns (Weight, Name, Code and Date). We then loop over the rows in the table. For each row, we call the Field() extension four times.DataTable

Tip: We can use an index argument (0, 2) to the Field extension. Or we can use a string argument ("Name", "Date").

Also: The Field extension is a Generic Function. We use a type argument, such as (Of Integer), with it. This indicates the return type.

VB.NET program that uses Field Function Module Module1 Sub Main() ' Create new table. Dim table As DataTable = New DataTable table.Columns.Add("Weight", GetType(Integer)) table.Columns.Add("Name", GetType(String)) table.Columns.Add("Code", GetType(String)) table.Columns.Add("Date", GetType(DateTime)) table.Rows.Add(57, "Koko", "A", DateTime.Now) table.Rows.Add(130, "Fido", "B", DateTime.Now) table.Rows.Add(92, "Alex", "C", DateTime.Now) table.Rows.Add(25, "Charles", "D", DateTime.Now) table.Rows.Add(7, "Candy", "E", DateTime.Now) ' Loop over rows. For Each row As DataRow In table.Rows ' Get fields. Dim weight As Integer = row.Field(Of Integer)(0) Dim name As String = row.Field(Of String)("Name") Dim code As String = row.Field(Of String)(2) Dim date1 As DateTime = row.Field(Of DateTime)("Date") ' Display fields. Console.WriteLine("{0} {1} {2} {3}", weight, name, code, date1) Next End Sub End Module Output 57 Koko A 6/10/2013 5:00:47 PM 130 Fido B 6/10/2013 5:00:47 PM 92 Alex C 6/10/2013 5:00:47 PM 25 Charles D 6/10/2013 5:00:47 PM 7 Candy E 6/10/2013 5:00:47 PM
Notes, casting. In this example, no casting is performed. Casting often leads to problems because of its confusing syntax and the extra steps needed. It makes code hard to read. With Field, objects are returned strongly-typed.
Summary. With DataRow, cells can be retried with the Items array. But the Field generic Function is an alternative, and sometimes superior, syntax form. It can lead to clearer code when using DataTable.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf