数组中的交换对象 - C# [英] Swapping Objects in an Array - C#
问题描述
在C#中,我有菜单项的Array。我试图用交换索引2和数组的索引3两个对象都没有成功以下code:
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;
必须有一个原因,第二个和第三个行不工作在C#我可能还不明白。有没有人能够澄清些吗?我一定要更深入和交换中的对象的每个属性分别?
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?
编辑 - 对不起。貌似我试图清理张贴时所做的code的一个烂摊子。现予以更正。
Edited - Sorry. Looks like I made a mess of the code when trying to clean it up for posting. Corrected now.
实际code是:
MenuItem TempButton = MenuItems.Items[SelectedButton.CountId];
MenuItems.Items[SelectedButton.CountId] = MenuItems.Items[SelectedButton.CountId + 1];
MenuItems.Items[SelectedButton.CountId + 1] = TempButton;
MenuItems.Items
是菜单项
放眼观看我已经放在MenuItems.Items,什么也没有发生在2线或3。
Looking at the Watch I have placed on MenuItems.Items, nothing happens on Line 2 or 3.
的 MenuItems.Items
酒店get和set功能,这可能是导致该问题...将进一步调查...
The MenuItems.Items
property has get and set functions, which may be causing the issue... Will investigate further...
推荐答案
您的设置产品[2]
到温度
,这是产品[2]
来首先,让你有效地没有做任何事情。我不知道是什么 SelectedButton.CountId
应该是。
You are settings Items[2]
to Temp
, which was Items[2]
to begin with, so you are effectively not doing anything. I don't know what SelectedButton.CountId
is supposed to be.
但如果你只是想要交换指数2和3,你可以这样做:
But if you just want to swap indices 2 and 3, you can do this:
Item Temp = Items[2];
Items[2] = Items[3];
Items[3] = Temp;
这篇关于数组中的交换对象 - C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!