2013-04-18 39 views
0

存在這裏是代碼:「GroupforA」這個名字不會在目前情況下

class Program 
{ 
    static void Main(string[] args) 
    { 

     List<Item> cont1 = new List<Item>(); 
     cont1.Add(objA); //objA is local varible that has been defined. 
     cont1.Add(objB); //objB is local varible that has been defined. 
     cont1.Add(objC); //objC is local varible that has been defined. 
     cont1.Add(objD); //objD is local varible that has been defined. 
     cont1.Add(objE); //objE is local varible that has been defined. 

     int count = cont1.Count; 

     List<Item> cont2 = GroupingA(cont1, count); 

     for (int i = 0; i < cont1.Count; i++) 
     { 
     Console.Write(cont1[i].Name + " ");//output the item's name. 

     } 
     Console.WriteLine(); 
     Console.Write("Container 2: "); 
     for (int i = 0; i < cont2.Count; i++) 
     { 
     Console.Write(cont2[i].Name + " ");//output the item's name. 
     } 
    } 
} 
public class GroupItem 
{ 
    public List<Item> GroupingA (List<Item> obj, int count) 
    { 
     List<Item> temp = new List<Item>(); 
     for (int i = 0; i < count; i++) 
     { 
      if (i == count - 1) 
      { 
       break; 
      } 
      else if (temp.Count > 0) 
      { 
       break; 
      } 
      else 
      { 
       for (int j = i + 1; j < count; j++) 
       { 
        bool equal = obj[i].EqualFirstPhase(obj[j]); 
        if (equal == true) 
        { 
         if (temp.Exists(element => element == temp[j])) 
         { 
          continue; 
         } 
         else 
         { 
          temp.Add(obj[j]); 
          if (temp.Exists(element => element == obj[i])) 
          { 
           continue; 
          } 
          else 
          { 
           temp.Insert(0, obj[i]); 
          } 
          i--; 

         } 

        } 
       } 

      } 
      for (int k = 0; k < count; k++) 
      { 
       for (int l = 0; l < temp.Count; l++) 
       { 
        if (obj[k].Name.Contains(temp[l].Name)) 
        { 
         obj.RemoveAt(k); 
         count--; 

        } 
       } 
       if (obj.Count < count) 
       { 
        k = 0; 

       } 
      } 


     } 
     return temp; 
    } 
} 

我想用GroupingA方法來重新組合成cont1cont2。但是,有一個錯誤。

名稱'GroupingA'在當前上下文中不存在。

任何人都可以幫我嗎? OOP真的很弱,特別是命名。

+0

我懷疑你的意思是'GroupingA'(也可能'GroupItem')是靜態的。如果您將'GroupingA'重新定義爲:'public static List GroupingA(List obj,int count);'並將'Main'方法中的原始調用更改爲:'List cont2 = GroupItem.GroupingA(cont1,計數);'它會編譯並運行。但我不太確定,如果這是你的意圖或不是靜態的。 – 2013-04-18 02:56:31

回答

0

你需要使用: -

GroupItem grpItem = new GroupItem(); 
List<Item> cont2 = grpItem.GroupingA(cont1, count); 

原因: -

你GroupingA方法是非靜態的和在類GroupingItem定義。並且您正嘗試從您的Program課程中訪問它,就好像方法GroupingA是該課程的一部分。要從其他類訪問非靜態方法,您需要Instantiate該類,並使用該對象來訪問該類。如果是靜態方法,可以使用該特定類的.運算符直接訪問它們。 ex Class.Method(...)

+0

謝謝,它的工作原理! 只是一個簡單的東西,但讓我頭疼尋求幫助。 – 2013-04-18 02:59:51

+1

@kingjia,如果這有助於您解決問題,請將此標記爲正確答案:) – 2013-04-18 03:02:57

+0

@kingjia很高興幫助。我會建議你閱讀更多關於面向對象的概念。 Hanlet,謝謝:) – PSL 2013-04-18 03:05:19

0

該行 List<Item> cont2 = GroupingA(cont1, count);正在Program中被調用。

如果沒有類名稱或對象標識符,說明方法的位置,它會在當前類中查找,在您的情況下,它是Program

你有幾種選擇。

  • 充分利用GroupingA方法靜態和替換List<Item> cont2 = GroupItem.GroupingA(cont1, count);

  • 行做GroupItem的一個實例,指的是不是:

    GroupItem g = new GroupItem(); 
    List<Item> cont2 = your.GroupingA(cont1, count); 
    
  • 移動方法GroupingAGroupItem類並將其放置在Program類中,以便您的線知道在哪裏找到它。
+0

兩者都是很好的答案!但是,自從它名列前茅以來,我就是第一個。對不起〜 – 2013-04-18 05:23:38

相關問題