Blue Theme Orange Theme Green Theme Red Theme
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Home | Forums | Videos | Photos | Blogs | Beginners | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
6 Months Free & No Setup Fees ASP.NET Hosting!
Search :       Advanced Search »
Home » VB.NET » VB.Net Lists: Add some elegance to your code

VB.Net Lists: Add some elegance to your code

A short and to-the-point tutorial that demonstrates how to sort and search using VB.Net's List object.

Page Views : 45142
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Team Foundation Server Hosting
Become a Sponsor
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

It is a fairly common programming scenario to find ourselves with a list of identical objects. In the past, without adequate support from programming languages, we found ourselves writing a lot of searching and sorting code, and that may have put you off using lists in favour of arrays. All that has changed with VB.Net - its implementation of a list makes handling such lists remarkably easy.

For example, given the following class Person:

Public Class Person

 

    Public age As Integer

    Public name As String

 

    Public Sub New(ByVal age As Integer, ByVal name As String)

 

        Me.age = age

        Me.name = name

 

    End Sub 'New

 

End Class 'Person


We can create a list of Person objects and add six people like so:

List<person>people = new List<person>();

people.Add(New Person(50, "Fred"))

people.Add(New Person(30, "John"))

people.Add(New Person(26, "Andrew"))

people.Add(New Person(24, "Xavier"))

people.Add(New Person(5, "Mark"))

people.Add(New Person(6, "Cameron"))

VB.Net's list mechanism provides us with a number of useful methods. Personally, I find ForEach, FindAll and Sort to be very useful. ForEach allows us access to each item in the list. FindAll allows us to search for objects in the list that match a specific condition. Sort allows us to sort the objects in the list. The following code demonstrates how we might use each of these methods:

Console.WriteLine("Unsorted list")

 

people.ForEach(Delegate Function(p As Person) As System.Char[])

If (True) Then

   Console.WriteLine([String].Format("{0} {1}", p.age, p.name)) '

End If

 

' Find the young

List<person> young = people.FindAll(delegate(Person p) { return p.age < 25; });

Console.WriteLine("Age is less than 25")

young.ForEach(Delegate Function(p As Person) As System.Char[])

If (True) Then

   Console.WriteLine([String].Format("{0} {1}", p.age, p.name))

End If

 

' Sort by name

Console.WriteLine("Sorted list, by name")

 

people.Sort(Delegate Function(p1 As Person, p2 As Person) As System.Char[])

If (True) Then

   Return p1.name.CompareTo(p2.name)

End If

 

people.ForEach(Delegate Function(p As Person) As System.Char[])

If (True) Then

   Console.WriteLine([String].Format("{0} {1}", p.age, p.name))

End If

 

' Sort by age

Console.WriteLine("Sorted list, by age")

people.Sort(Delegate Function(p1 As Person, p2 As Person)

If (True) Then

   Return p1.age.CompareTo(p2.age)

End If

 

people.ForEach(Delegate Function(p As Person))

If (True) Then

   Console.WriteLine([String].Format("{0} {1}", p.age, p.name))

End If

And here is the output that we should expect:

Unsorted list
50 Fred
30 John
26 Andrew
24 Xavier
5 Mark
6 Cameron

Age is less than 25
24 Xavier
5 Mark
6 Cameron

Sorted list, by name
26 Andrew
6 Cameron
50 Fred
30 John
5 Mark
24 Xavier

Sorted list, by age
5 Mark
6 Cameron
24 Xavier
26 Andrew
30 John
50 Fred

Lists are powerful and result in fewer, and more elegant, lines of code. Hopefully this short example has demonstrated their ease and you will find yourself using them in your day-to-day development activities.

NOTE: THIS ARTICLE IS CONVERTED FROM C# TO VB.NET USING A CONVERSION TOOL. ORIGINAL ARTICLE CAN BE FOUND ON C# CORNER (http://www.c-sharpcorner.com/). 

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Craig Murphy
Craig Murphy is employed by a leading professional services consulting firm as a Systems Development Engineer. Craig is also an author, developer, speaker, Microsoft Most Valuable Professional and Certified ScrumMaster. He specialises in: XML, web services, XSLT, TDD, .net and XP. He has extensive experience with Borland Delphi spanning some 10 years. Apart from in-house software, Craig has written applications for major oil companies and local councils. He is currently using C# and Visual Studio 2005.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
 Comments
C# to VB.Net by Steven On October 17, 2008
Thanks for pointing me in the right direction, however the correct VB.Net syntax to create your list would be: Dim people As List(Of person) = New List(Of person) Steve
Reply | Email | Modify 
Nice by kanati On February 5, 2010
Good stuff though you are mixing C# and VB.NET syntax.  Bit confusing if someone isn't fluent in both and aware of it.   :)
Reply | Email | Modify 
Awesome by Livan On March 9, 2010
itu adalah LINQ ya ? keren

that is 'LINQ things', isn't it ? that is very cool
Reply | Email | Modify 
Discover the top 5 tips for understanding .NET Interop
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.