Sam

Sam

  • NA
  • 166
  • 0

Extracting / manipulating a record from a list by its index

Aug 28 2009 3:31 PM

Hello,
My c# prog generates lists of items (records).
I need to manipulate items in the list by their index.
While there are numerous list commands and methods, I could not find any that will extract or enable enter an item in a generated objects list by its index.
As an alternative I could think of converting a list item into array, manipulate the array and converting back to the list.
Exmple:

List<Customers> customers = new List<Customers>();
customers.Add(new Customers("Dan", "2334", "Mac", "G5 DC"));
customers.Add(new Customers("Abe", "1234", "IBM", "Thinkpad R60"));
.
.
customers.Add(new Customers("Sam", "1455", "Intel", "DV33"));
Can anyone help?
Attached is a working snippet.
Sam
 
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
Lists_sample
{
class Program
{
static void Main(string[] args)
{
Customers customer = new Customers();
customer.CustList();
Console.Read();
}
}
class Customers
{
string CustName;
string CustID;
string CustProdMake;
string CustProdType;
public Customers() { }
public Customers(string CustName, string CustID, string CustProdMake, string CustProdType)
{
this.CustName = CustName;
this.CustID = CustID;
this.CustProdMake = CustProdMake;
this.CustProdType = CustProdType;
}
public void CustList()
{
List<Customers> customers = new List<Customers>();
customers.Add(
new Customers("Dan", "2334", "Mac", "G5 DC"));
customers.Add(
new Customers("Abe", "1234", "IBM", "Thinkpad R60"));
customers.Add(
new Customers("Sam", "1455", "Intel", "DV33"));
customers.Add(
new Customers("Sharon", "1356", "Lenovo", "Thinkpad T44"));
customers.Add(
new Customers("Gale", "1111", "Dell", "CX280"));
Console.WriteLine("Current customers:");
Console.WriteLine("Name ID Product Make Product Type");
customers.ForEach(
delegate(Customers c)
{
Console.WriteLine(String.Format("{0,-12}{1,-12}{2,-12}{3,-12}",
c.CustName, c.CustID, c.CustProdMake, c.CustProdType));
});
}
}
}
 
 

 
 
 

Answers (3)