存在這裏是代碼:「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
方法來重新組合成cont1
cont2
。但是,有一個錯誤。
名稱'GroupingA'在當前上下文中不存在。
任何人都可以幫我嗎? OOP真的很弱,特別是命名。
我懷疑你的意思是'GroupingA'(也可能'GroupItem')是靜態的。如果您將'GroupingA'重新定義爲:'public static List- GroupingA(List
- obj,int count);'並將'Main'方法中的原始調用更改爲:'List
- cont2 = GroupItem.GroupingA(cont1,計數);'它會編譯並運行。但我不太確定,如果這是你的意圖或不是靜態的。 –
2013-04-18 02:56:31