Delete an item from a list on the basis of a condition

below is the code of my class. The code creates an ArrayList. It then adds a number of “PipesList” to the ArrayList, adding Pipes within each list.

I want to write a method -RemoveTheSmallPipes to get rid of all of the pipes with lengths less than 19. For which, I have written a piece of code which I don't know works or not! as the code throws an error:

Compiler Error Message: CS0050: Inconsistent accessibility: return type 'System.Collections.Generic.List' is less accessible than method 'Program.RemoveTheSmallPipes(System.Collections.Generic.List)'

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for Class1
/// </summary>




   public class Program
    {
        static void Main(string[] args)
        {
            List<PipesList> lstPipeTypes = new List<PipesList>();

        lstPipeTypes.Add(new PipesList("PVC Pipes"));
        lstPipeTypes[0].Pipes.Add(new Pipe("The blue pipe", 12));
        lstPipeTypes[0].Pipes.Add(new Pipe("The red pipe", 15));
        lstPipeTypes[0].Pipes.Add(new Pipe("The silver pipe", 6));
        lstPipeTypes[0].Pipes.Add(new Pipe("The green pipe", 52));

        lstPipeTypes.Add(new PipesList("Iron Pipes"));
        lstPipeTypes[1].Pipes.Add(new Pipe("The gold pipe", 9));
        lstPipeTypes[1].Pipes.Add(new Pipe("The orange pipe", 115));
        lstPipeTypes[1].Pipes.Add(new Pipe("The pink pipe", 1));

        lstPipeTypes.Add(new PipesList("Chrome Pipes"));
        lstPipeTypes[2].Pipes.Add(new Pipe("The grey pipe", 12));
        lstPipeTypes[2].Pipes.Add(new Pipe("The black pipe", 15));
        lstPipeTypes[2].Pipes.Add(new Pipe("The white pipe", 19));
        lstPipeTypes[2].Pipes.Add(new Pipe("The brown pipe", 60));
        lstPipeTypes[2].Pipes.Add(new Pipe("The peach pipe", 16));


        lstPipeTypes = RemoveTheSmallPipes(lstPipeTypes);

        foreach (var pipeList in lstPipeTypes)
        {
            Console.WriteLine("PipesList: {0}", pipeList.pipeType);

            foreach (var pipe in pipeList.Pipes)
            {
                Console.WriteLine("{0}, length: {1}", pipe.name, pipe.length);
            }
            Console.WriteLine();
        }

        Console.WriteLine("Done, press return to exit");
        Console.ReadLine();
    }


    public static List<PipesList> RemoveTheSmallPipes(List<PipesList> lstPipeTypes)
    {

        //Place your code in here
        //It should remove all pipes that have lengths lower than 19.


        foreach (var pipeList in lstPipeTypes)
        {

            foreach (var pipe in pipeList.Pipes)
            {

                    lstPipeTypes.RemoveAll(i => pipe.length < 19);

            }

        }

        return lstPipeTypes;

    }
}

class PipesList
{
    public string pipeType;
    public List<Pipe> Pipes;

    public PipesList(string newBoxType)
    {
        pipeType = newBoxType;
        Pipes = new List<Pipe>();
    }
}

class Pipe
{
    public string name;
    public float length;

    public Pipe(string newName, float newLength)
    {
        this.name = newName;
        this.length = newLength;
    }
}

Answers


Your PipesList class is internal, so it's only visible to other code within the same assembly. Your RemoveTheSmallPipes method is public, but refers to PipesList in its signature. That's not allowed.

Either make RemoveTheSmallPipes method internal, or make PipesList public.

Your implementation is also somewhat odd, by the way. It's unclear why you've got two levels of loops and a RemoveAll call, but the fact that you don't use your lambda expression parameter (i) within the body of the lambda expression is very suspicious. It's not clear what you're trying to do, but I don't think your code does what you expect at the moment...

EDIT: Based on your description, I suspect the body should look like this:

foreach (var pipeList in lstPipeTypes)
{
    pipeList.Pipes.RemoveAll(pipe => pipe.length < 19);
}

Your Pipe and PipesList classes aren't public, yet are being returned by a public method.


Class PipesList is internal (by default) but RemoveTheSmallPipes() is public.

Either make class PipesList public, or RemoveTheSmallPipes() internal.


  1. The compiler shows the following error:"Inconsistent accessibility: return type System.Collections.Generic.List is less accessible than method Program.RemoveTheSmallPipes(System.Collections.Generic.List)"This refers to the visibility of the respective types: Your return type List<PipesList> is internal (because the generic argument PipesList is internal), but your method is public (i.e. more visible than internal). Make your method internal, then this compiler error message should disappear.
  2. Your RemoveTheSmallPipes method is probably going to throw an exception; you should not change collections while iterating over them.

Need Your Help

Powershell Log Off Remote Session

powershell rdp

I am trying to formulate a Powershell command to remotely log off a user. We have a terminal server with a very unstable program that sometimes locks sessions. We have to remotely log off a user bu...

Not able to view the uploaded file on browser

node.js express

I have a working code - when it comes to uploading the file ::