Server Intellect Cloud Hosting

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!