交换数组中的对象 - C#
在 C# 中,我有一个 MenuItem 数组。我试图使用下面的代码交换数组索引 2 和索引 3 中的两个对象,但没有成功:
MenuItem Temp = Items[2];
Items[2] = Items[3];
Items[3] = Temp;
第二行和第三行在 C# 中不起作用一定有原因,我可能还不明白。有人能澄清一下吗?我是否必须更深入地单独交换对象中的每个属性?
已编辑 - 抱歉。看起来我在尝试清理代码以进行发布时把代码弄乱了。现已更正。
实际代码是:
MenuItem TempButton = MenuItems.Items[SelectedButton.CountId];
MenuItems.Items[SelectedButton.CountId] = MenuItems.Items[SelectedButton.CountId + 1];
MenuItems.Items[SelectedButton.CountId + 1] = TempButton;
MenuItems.Items
是 MenuItem
的数组
查看我放置在 MenuItems.Items 上的手表,第 2 行或第 3 行没有任何反应
。 >MenuItems.Items 属性具有 get 和 set 函数,这可能会导致问题...将进一步调查...
In C#, I have an Array of MenuItem. I'm trying to swap the two Objects in index 2 and index 3 of the array without success using the code below:
MenuItem Temp = Items[2];
Items[2] = Items[3];
Items[3] = Temp;
There must be a reason why the second and third lines aren't working in C# which I may not understand yet. Is anyone able to clarify it a bit more? Do I have to go deeper and swap each property in the objects individually?
Edited - Sorry. Looks like I made a mess of the code when trying to clean it up for posting. Corrected now.
The actual code is :
MenuItem TempButton = MenuItems.Items[SelectedButton.CountId];
MenuItems.Items[SelectedButton.CountId] = MenuItems.Items[SelectedButton.CountId + 1];
MenuItems.Items[SelectedButton.CountId + 1] = TempButton;
MenuItems.Items
is an array of MenuItem
Looking at the Watch I have placed on MenuItems.Items, nothing happens on Line 2 or 3.
The MenuItems.Items
property has get and set functions, which may be causing the issue... Will investigate further...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您将
Items[2]
设置为Temp
,最初是Items[2]
,因此您实际上没有执行任何操作。我不知道SelectedButton.CountId
应该是什么。但如果你只想交换索引 2 和 3,你可以这样做:
You are settings
Items[2]
toTemp
, which wasItems[2]
to begin with, so you are effectively not doing anything. I don't know whatSelectedButton.CountId
is supposed to be.But if you just want to swap indices 2 and 3, you can do this:
SelectedButton.CountId
= 2 吗?如果是这样我会尝试这个:注意最后一行有一个 3。
这样会更清楚:
Is
SelectedButton.CountId
= 2? if so I would try this:Note the last line has a 3 in it.
This would be clearer:
我不知道
SelectedButton.CountId
应该是什么,但您将Temp
放回到最初的同一个插槽中。MenuItems.Items
似乎是与 Items 完全不同的集合。I have no idea what
SelectedButton.CountId
is supposed to be but you're puttingTemp
right back in the same slot it was to begin with. AndMenuItems.Items
seems to be a totally different collection from Items.尝试:
这应该以气泡方式交换
try:
This should swap in a bubble fashion
我记得前段时间遇到过类似的混乱来源,使用
DataRow.ItemArray
属性。该属性极其违反直觉,其原因与您示例中的Items
属性看起来如此奇怪的原因相同。最终令人困惑的是,该属性被设计为复制并分配给,就像您通常使用值类型的字段一样(例如
int
、双
等)。也就是说,要更改索引 2 处的元素,这是行不通的:上面的代码本质上是将行中的值复制到新数组中,获取该副本并将索引 2 处的值设置为“新值”,然后新数组将立即超出范围。 在我的书中,该属性应该的工作方式是:
非常违反直觉(库开发人员请注意:不要这样做)。但听起来这可能是您在代码中看到的问题背后的问题。
换句话说,我认为您(现在)的交换代码是正确的。问题在于谁有聪明的主意,可以让
Items
属性表现得像一个值字段。I remember running into a similar source of confusion some time ago, with the
DataRow.ItemArray
property. This property was extremely counterintuitive for the very same reason that theItems
property in your example seems so odd.What was ultimately so confusing was that the property was designed to be copied from and assigned to, just like you normally would with a field of value type (like
int
,double
, etc.). That is, to change the element at index 2, this would not work:The above code would essentially copy the values from the row into a new array, take that copy and set the value at index 2 to "New Value," and then the new array would immediately be out of scope. The way this property was supposed to work was:
Very counterintuitive, in my book (note to library developers: don't do this). But it sounds like this is probably the problem behind the issue you were seeing with your code.
In other words, I think the swapping code you have (now) is correct. The problem lies with whoever had the bright idea of making that
Items
property behave as if it's a value field.我遇到了同样的问题,因为我想上下移动 WPF-TreeView 中的元素。由于没有一个答案可以解决我的问题,所以这里是我能找到的最好的答案。
这解决了集合中元素的分配问题。此外,它的优点是当前选择的项目永远不会被触摸并保持选择状态。
I had the same problem as I wanted to move elements in a WPF-TreeView up and down. Since none of the answers solved the problem for me here is the best I could find.
This solves the problem of assigning elements in the collection. Further it has the advantage that the current seleced item is never touched and stays selected.
解决了问题。我觉得我把问题搞得太复杂了。
MenuItems.Items 是一个具有 get/set 函数的属性,可返回/设置私有 ArrayList。
我在 MenuItems 类中创建了一个函数,它交换了私有 ArrayList 中的索引。 (它使用的标准交换代码类似于我尝试过的以及每个人在回复中提到的内容。)
感谢大家的帮助。
Solved the issue. I think I complicated the problem a bit too much.
MenuItems.Items was a property with get/set functions that returns/sets a private ArrayList.
I created a function in the class for MenuItems which swapped the indexes in the private ArrayList. (which used standard swapping code similar to what I tried and what everyone has mentioned in their replies.)
Thanks for everyone's help.