Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Winforms 如何在堆栈中表示集合加整数?_Winforms_Design Patterns - Fatal编程技术网

Winforms 如何在堆栈中表示集合加整数?

Winforms 如何在堆栈中表示集合加整数?,winforms,design-patterns,Winforms,Design Patterns,我们使用的是一个简单的“后退一步”按钮。我们将收藏推到堆栈上,当他回击一个时,我们再次将它们弹出 今天,我们想把它扩展一点。当我们将集合推送到堆栈上时,我们希望在集合旁边存储一个整数。然后,当我们查看堆栈时,我们希望检索集合及其整数 将集合加上整数放入堆栈的最佳方式是什么 我们应该为collection plus integer定义一个结构或类吗?或者其他方法?最简单的现成解决方案是使用整数扩展当前容器: public class StackElement : CustomerCollectio

我们使用的是一个简单的“后退一步”按钮。我们将收藏推到堆栈上,当他回击一个时,我们再次将它们弹出

今天,我们想把它扩展一点。当我们将集合推送到堆栈上时,我们希望在集合旁边存储一个整数。然后,当我们查看堆栈时,我们希望检索集合及其整数

将集合加上整数放入堆栈的最佳方式是什么


我们应该为collection plus integer定义一个结构或类吗?或者其他方法?

最简单的现成解决方案是使用整数扩展当前容器:

public class StackElement : CustomerCollection
{ 
   public int Version {get; set;}
}
或使用整数进行合成:

public class StackElement
{ 
   public CustomerCollection Customers {get; set;}
   public int Version {get; set;}
}
然后像使用堆栈中的任何其他东西一样使用它:

Stack<StackElement> stack = new Stack<StackElement>();
stack.Push(...)
var stackElement = stack.Pop();
Stack Stack=新堆栈();
stack.Push(…)
var stackElement=stack.Pop();

您不需要为此创建新类。您可以像这样简单地将您的值添加到堆栈中

stack.Push(new KeyValuePair<int, Collection>(yourInteger, yourCollection));
KeyValuePair<int, Collection> valueYouWant = stack.Pop();

valueYouWant.Key   --> Your integer
valueYouWant.Value --> Your collection
stack.Push(新的KeyValuePair(yourInteger,yourCollection));
像这样得到它们

stack.Push(new KeyValuePair<int, Collection>(yourInteger, yourCollection));
KeyValuePair<int, Collection> valueYouWant = stack.Pop();

valueYouWant.Key   --> Your integer
valueYouWant.Value --> Your collection
keyvaluepairvalueyouwant=stack.Pop();
valueYouWant。输入-->您的整数
值您想要的值。值-->您的集合

是的,为此定义一个结构或类。任何集合,或某个专门的集合?@SWeko它是一个客户列表,继承组件并实现一系列交互,例如IBindingListView等。这不要求整数是唯一的吗?他们不会在我们的情况下不。整数可能根本不是唯一的。请注意,
堆栈
不是
字典
。你可以推动这样的事情
stack.Push(新的键值对(integer1,collection1));stack.Push(新的键值对(integer1,collection1))