Fun with C# Lists (Final Part)

Category: .NET Framework

Fun with C# Lists… both of them. Part Three of Three

This article is the Final Part in the series. Read Part 2 here

Introduction

In this final article of our series on using Lists we will be working with List< Type> some more and showing you how we take the values from within the List and display those values in a List Box so you can visually see the results of each action.

If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.


Implementation

The final implementation for this example is the “DisplayList” method we created so we can visually see the results of everything we have done so far.  We will then show you the source code for the completed application so you can see how everything works together.
There are three radio buttons that control the results in this example.  You simply need to select the sort option you wish to view and then select the “Sort” button and view the results.  The options are:

 

 

  • Seats less than 5 people
  • Sorts by Seating Capacity
  • Sorts by Model

DisplayList Method

This method was designed so we can visually show you how the results of our examples look real time.  We are simply taking the List<Type> that we have populated with the Models, and iterating through that List and populating the List Box that is on our example form.

private void DisplayList(List<Automobile> autoList)
{
lstResults.Items.Clear();
foreach (Automobile a in autoList)
{
lstResults.Items.Add(a.model);
}
}

The List Box will be cleared before each call to this method and then be re-populated based on the current sort order of the List.

We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!

Source Code:

The following is the entire example application an shows how all of the examples mentioned above come together and formulate the application. 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace FunWithLists
{
public partial class Form1 : Form
{
List<Automobile> auto = new List<Automobile>();
public Form1()
{
InitializeComponent();
}
private void btnSort_Click(object sender, EventArgs e)
{
if (rbModel.Checked)
{
auto.Sort(delegate(Automobile auto1, Automobile auto2) { return auto1.model.CompareTo(auto2.model); });
lblSortType.Text = "Model Name";
DisplayList(auto);
}
else if(rbSeatCapacity.Checked)
{
auto.Sort(delegate(Automobile auto1, Automobile auto2) { return auto1.seats.CompareTo(auto2.seats); });
lblSortType.Text = "Seating Capacity Ascending";
DisplayList(auto);
}
else if (rbSeats.Checked)
{
List<Automobile> results = auto.FindAll(delegate(Automobile auto1) { return auto1.seats < 5; });
lblSortType.Text = "Less than 5 Seats";
DisplayList(results);
}
else
{
MessageBox.Show("Please select an option.", "Unknown Selection");
}
}
private void Form1_Load(object sender, EventArgs e)
{
auto.Add(new Automobile(2, "Corvette"));
auto.Add(new Automobile(4, "Mustang"));
auto.Add(new Automobile(5, "Accord"));
auto.Add(new Automobile(3, "F-150"));
auto.Add(new Automobile(7, "Entourage"));
auto.Add(new Automobile(8, "Pilot"));
lblSortType.Text = "Unsorted";
DisplayList(auto);
}
private void DisplayList(List<Automobile> autoList)
{
lstResults.Items.Clear();
foreach (Automobile a in autoList)
{
lstResults.Items.Add(a.model);
}
}
}
}
public class Automobile
{
public int seats;
public string model;
public Automobile(int seats, string model)
{
this.seats = seats;
this.model = model;
}
}

 

 

 

Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.


Other Methods

Here is a list of other methods that can be found in the new List<Type> class:

 

  • Count
  • Find
  • ForEach
  • Reverse

What have we learned?

  • Creating the Automobile class
  • How to implement List<Type>
  • How to find all occurrences of a value based on criteria
  • How to Populate the List
  • How to sort the list by Model
  • How to Sort the List by Seating Capacity
  • Sort and Display the List of Automobiles with less than 5 available seats

This is the end of this article series. Read Part 1 here

Attachments



Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!

Leave a Comment

Comments on this Article

Post a Comment
Name:
Website:
Email:
Comments:

#1 Homes for Sale Kennewick

Posted By: Homes for Sale Kennewick | 11.24.2009 at 3:14 AM

Hey this is fun. I tried it & it works perfect.Thanks for sharing the codes.

#2 game online

Posted By: game online | 3.07.2010 at 3:39 AM

Hi admin, thank you very much for sharing this information.. I found it so cool. Take care, William!

#3 310-202

Posted By: 310-202 | 4.07.2010 at 1:59 AM

Hi admin, thank you very much for sharing this information.. I found it so cool. Take care, William!

#4 mbt shoes

Posted By: mbt shoes | 5.22.2010 at 4:33 AM

Do not want to foot pain shopping a day, please do wear MBT uncertainly in mind shoes, soft, smooth surface, is a powerful daily training shoes. mbt shoes feature is also a friend introduced to me, wore one day know, really go a day will not feel tired, It is really a miracle. if I wear flat heeled shoes to go shopping, visiting the day, my leg still pain, but wearing this MBT Shoes,can improve gait and reduce the pressure on the floor right foot.

#5 coach handbags

Posted By: coach handbags | 5.24.2010 at 3:23 AM

The Far East aristocrat becomes fashion new influence … “the Russian always to choose best goods - - Chanel in the best brand the handbag and the coat,

#6 cheap mbt shoes

Posted By: cheap mbt shoes | 6.18.2010 at 9:08 PM

good post!!thank you

#7 wholesale mlb jerseys

Posted By: wholesale mlb jerseys | 7.10.2010 at 5:42 AM

Thank you for your sharing.I'm very interested in <a href="http://www.hotmlbjerseys.com"> wholesale mlb jerseys</a>.There are so many famouse mlb stars jerseys,we have <a href="http://www.hotmlbjerseys.com/new-york-yankees-derek-jeter-jersey-c-33.html"> New York Yankees Derek Jeter Jersey</a>.So I an fascinated to the <a href="http://www.hotmlbjerseys.com/philadelphia-phillies-chase-utley-jersey-c-34.html"> Philadelphia Phillies Chase Utley Jersey</a>,no matter any one of mlb stars juse like <a href="http://www.hotmlbjerseys.com/minnesota-twins-joe-mauer-jersey-c-35.html"> Minnesota Twins Joe Mauer Jersey</a> and <a href="http://www.hotmlbjerseys.com/boston-redsox-dustin-pedroia-jersey-c-36.html"> Boston RedSox Dustin Pedroia Jersey</a>,which are my best witness as the <a href="http://www.hotmlbjerseys.com/st-louis-cardinals-albert-pujols-jersey-c-37.htmll"> St. Louis Cardinals Albert Pujols Jersey</a> fans!

#8 wholesale nfl jerseys

Posted By: wholesale nfl jerseys | 7.10.2010 at 5:44 AM

It was a very nice idea! Just wanna say thank you for the information you have shared [url=www.shopping-jerseys.com] wholesale nfl jerseys[/url].

#9 cheap ugg

Posted By: cheap ugg | 7.16.2010 at 10:11 PM

Undoubtedly, it is the best to wear Sheepskin boot in the winter. All our uggs on sale features the finest twin-faced sheepskin for the ultimate comfort. it will look great to match all clothes, such as T-shirt, jeans, shorts and dresses. We provide the high quality and cheap UGG for you. Go to shop for UGGs on sale. There are many styles in our UGG Boots Sale.You will get unique and timeless comfort and warmth in Uggs form Our UGG Outlet. susan-inbusiness@hotmail.com

#10 tiffany co

Posted By: tiffany co | 7.21.2010 at 3:05 AM

For classic and quality genuine silver jewelry, many people choose Tiffany & Co brand.Now,we present you the hottest Tiffany silver jewelry .get more http://www.tiffanyonsale.com/

#11 tiffany co

Posted By: tiffany co | 7.21.2010 at 3:05 AM

For classic and quality genuine silver jewelry, many people choose Tiffany & Co brand.Now,we present you the hottest Tiffany silver jewelry .get more http://www.tiffanyonsale.com/

#12 tiffany jewelry

Posted By: tiffany jewelry | 7.21.2010 at 3:05 AM

For classic and quality genuine silver jewelry, many people choose Tiffany & Co brand.Now,we present you the hottest Tiffany silver jewelry .get more http://www.tiffanyonsale.com/

#13 tiffany jewellery

Posted By: tiffany jewellery | 7.21.2010 at 3:05 AM

For classic and quality genuine silver jewelry, many people choose Tiffany & Co brand.Now,we present you the hottest Tiffany silver jewelry .get more http://www.tiffanyonsale.com/

#14 congcong

Posted By: congcong | 7.21.2010 at 4:40 AM

[url=http://www.imitatewatch.com/GoodsDetail/Replica-Hublot-Big-Bang-Mens-Watch-301-SB-131-RX-Watches-2213.html]Replica Hublot Big Bang Watches[/url]

[url=http://www.imitatewatch.com/GoodsSeries/Replica-Luminor-Watches-311.html]Replica Panerai Luminor Watches[/url]

[url=http://www.imitatewatch.com/GoodsSeries/Replica-Luminor-Watches-311.html]Patek Philippe Celestial Watches[/url]

[url=http://www.imitatewatch.com/GoodsSeries/Replica-Daytona-Watches-363.html]Replica Rolex Daytona Watches[/url]

[url=http://www.imitatewatch.com/GoodsSeries/Replica-Pilot-Watches-248.html]Replica IWC Pilot Watches[/url]

[url=http://www.imitatewatch.com/GoodsSeries/Replica-Portuguese-Watches-250.html]Replica IWC Portuguese Watches[/url]

[url=http://www.imitatewatch.com/GoodsSeries/Replica-Royal_Oak_Offshore-Watches-29.html]Audemars Piguet Royal Oak Offshore Replica Watches[/url]

[url=http://www.imitatewatch.com/GoodsSeries/Replica-Happy_Sport-Watches-179.html]Chopard Happy Sport Replica Watches[/url]

#15 gucci bags

Posted By: gucci bags | 7.29.2010 at 10:52 PM

thank you

#16 Abercrombie and fitch

Posted By: Abercrombie and fitch | 8.10.2010 at 2:18 AM

I agree with your point of view, thank you for your article!

#17 chanel bracelet

Posted By: chanel bracelet | 8.20.2010 at 4:27 AM

miracle. if I wear flat heeled shoes to go shopping, visiting the day, my leg still pain, but wearing this MBT Shoes,can improve gait and reduce the pressure on the floor right foot.

#18 chanel bracelet

Posted By: chanel bracelet | 8.20.2010 at 4:28 AM

miracle. if I wear flat heeled shoes to go shopping, visiting the day, my leg still pain, but wearing this MBT Shoes,can improve gait and reduce the pressure on the floor right foot.

#19 replica watches

Posted By: replica watches | 8.30.2010 at 12:03 PM

good

#20 shanne

Posted By: shanne | 8.31.2010 at 10:24 PM

www.cheap4nfljerseys.com

www.cheap4nfljerseys.com

www.cheap4nfljerseys.com

#21 shanne

Posted By: shanne | 8.31.2010 at 10:32 PM

www.cheap4nfljerseys.com

www.cheap4nfljerseys.com

www.cheap4nfljerseys.com

www.cheap4nfljerseys.com

#22 radio station transmitter

Posted By: radio station transmitter | 9.07.2010 at 2:04 AM

nice job indeed! it was especially interesting and useful for me to read about user groups. not thinking about getting involved into SharePoint Community, just curious so far. hope to learn more