I have got the problem with creating an array list in c#, Can you please help me.
I need to create the array list to remove all the pipes that have lengths lower than 19.
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ArrayList lstPipeTypes = new ArrayList();
lstPipeTypes.Add(new PipesList("PVC Pipes"));
((PipesList)lstPipeTypes[0]).Pipes.Add(new Pipe("The blue pipe", 12));
((PipesList)lstPipeTypes[0]).Pipes.Add(new Pipe("The red pipe", 15));
((PipesList)lstPipeTypes[0]).Pipes.Add(new Pipe("The silver pipe", 6));
((PipesList)lstPipeTypes[0]).Pipes.Add(new Pipe("The green pipe", 52));
lstPipeTypes.Add(new PipesList("Iron Pipes"));
((PipesList)lstPipeTypes[1]).Pipes.Add(new Pipe("The gold pipe", 9));
((PipesList)lstPipeTypes[1]).Pipes.Add(new Pipe("The orange pipe", 115));
((PipesList)lstPipeTypes[1]).Pipes.Add(new Pipe("The pink pipe", 1));
lstPipeTypes.Add(new PipesList("Chrome Pipes"));
((PipesList)lstPipeTypes[2]).Pipes.Add(new Pipe("The grey pipe", 12));
((PipesList)lstPipeTypes[2]).Pipes.Add(new Pipe("The black pipe", 15));
((PipesList)lstPipeTypes[2]).Pipes.Add(new Pipe("The white pipe", 19));
((PipesList)lstPipeTypes[2]).Pipes.Add(new Pipe("The brown pipe", 60));
((PipesList)lstPipeTypes[2]).Pipes.Add(new Pipe("The peach pipe", 16));
RemoveTheSmallPipes(lstPipeTypes);
}
public static ArrayList RemoveTheSmallPipes(ArrayList lstPipeTypes)
{
//should remove all pipes that have lengths lower than 19.
return lstPipeTypes;
}
}
class PipesList
{
public string pipeType;
public ArrayList Pipes;
public PipesList(string newBoxType)
{
pipeType = newBoxType;
Pipes = new ArrayList();
}
}
class Pipe
{
public string name;
public float length;
public Pipe(string newName, float newLength)
{
this.name = newName;
this.length = newLength;
}
}
}
I have created the two classes called Pipe and PipeList as well and I need to place the array list in the “RemoveTheSmallPipes” method. But I am confused now to write the rest. Please help me to remove all the pipes that have lengths lower than 19.
Here’s your method without changing anything else:
It’s really a shame You cannot change anything else because this code is not up to standards anymore. To show You a difference here’s modified C# 4.0 version:
Console:
Classes:
As You can see it’s quite different, but much more elegant (Linq FTW! 🙂 )