2012-06-11 53 views
0

下面是我班的代碼。該代碼創建一個ArrayList。然後它將一些「PipesList」添加到ArrayList中,在每個列表中添加管道。根據條件從列表中刪除項目

我想寫一個方法-RemoveTheSmallPipes來擺脫長度小於19的所有管道。爲此,我寫了一段我不知道有效的代碼!作爲代碼拋出錯誤:

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; 
    } 
} 
+0

這個問題是初步測試的一部分,我的公司(Xibis,www.xibis.com,我們在右邊做廣告>>)在開發者候選人到他們面前進行更復雜的測試後,確保他們具有基礎開發技能的基礎,所以我們不會浪費時間或候選人時間。對於考慮使用以下答案的任何考生,我們都知道這些答案,所以如果您以自己的身份提交答案,您將不會被邀請參加內部考試。請嘗試自行回答問題。 –

+0

我無法得到答案。我在這裏問了這個問題,這樣我就可以知道如何解決這樣的錯誤,並且如果我面臨類似的錯誤,可以記住將來的錯誤。 –

+0

我投票結束這個問題作爲題外話,因爲它是我們的公司初步測試的一部分,我們在邀請他們進行內部測試之前給予考生。 –

回答

4

PipesList類是internal,所以它只能在同一組件中的其他代碼可見。您的RemoveTheSmallPipes方法是public,但在其簽名中指PipesList。這是不允許的。

可以使RemoveTheSmallPipes方法內部公開,或使PipesList公開。順便說一句,你的實現也有點奇怪。目前還不清楚爲什麼你有兩個級別的循環調用 a RemoveAll調用,但是您不在lambda表達式的主體內使用lambda表達式參數(i)這一事實非常可疑。目前還不清楚你在做什麼,但我認爲你的代碼不會做你期望的那一刻......

編輯:根據你的描述,我懷疑身體應該看起來像這樣:

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

@Tigran:默認情況下不應該保密嗎? –

+0

我試圖使用循環訪問列表,然後通過使用lambda表達式,條件是如果管道的長度小於19,管道項目應該從列表中刪除。 –

+0

@SidP:看我的編輯。目前,您在PipeLists列表中調用'RemoveAll',而不是在管道列表中調用... RemoveDll ... –

0

Your Pipe和PipesList類不公開,但是正在通過公共方法返回。

0

類PipesList是內部的(默認情況下),但RemoveTheSmallPipes()是公共的。

要麼使類PipesList公共或RemoveTheSmallPipes()內部。

0
  1. 編譯器顯示以下錯誤:

    「可訪問性不一致:返回類型System.Collections.Generic.List比方法Program.RemoveTheSmallPipes(System.Collections.Generic.List)不易接近」

    這是指相應類型的可見性:您的返回類型List<PipesList>內部(因爲通用參數PipesList內部),但您的方法是公衆(即比內部更可見)。使你的方法內部,那麼這個編譯器錯誤消息應該消失。

  2. 您的RemoveTheSmallPipes方法可能會拋出異常;您不應該在迭代它們時更改集合。