开发者

Swapping Objects in an Array - C#

开发者 https://www.devze.com 2023-01-14 14:14 出处:网络
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:

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.CountI开发者_如何学JAVAd];  
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...


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.

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;


Is SelectedButton.CountId = 2? if so I would try this:

Item Temp = MenuItems.Items[2];  
MenuItems.Items[SelectedButton.CountId] = MenuItems.Items[3];  
MenuItems.Items[3] = Temp;  

Note the last line has a 3 in it.

This would be clearer:

Item Temp = MenuItems.Items[SelectedButton.CountId];  
MenuItems.Items[SelectedButton.CountId] = MenuItems.Items[3];  
MenuItems.Items[3] = Temp;  


I have no idea what SelectedButton.CountId is supposed to be but you're putting Temp right back in the same slot it was to begin with. And MenuItems.Items seems to be a totally different collection from Items.

string[] items = { "one", "two", "three" };
string temp = items[1]; // temp = "two"
items[1] = items[2]; // items[1] = "three"
items[2] = temp; // items[2] = "two"

// items is now
// { "one", "three", "two" }


try:

Item Temp = Items[SelectedButton.CountId];   
Items[SelectedButton.CountId] = MenuItems.Items[SelectedButton.CountId+1];   
Items[SelectedButton.CountId+1] = Temp;  

This should swap in a bubble fashion


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 the Items 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:

row.ItemArray[2] = "New Value";

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:

object[] items = row.ItemArray;
items[2] = "New Value";
row.ItemArray = items;

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.


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.

    private void MoveLayerUp()
    {
        if(Layers.SelectedItem != null)
        {
            int index = Layers.Items.IndexOf(Layers.SelectedItem);
            if (index > 0)
            {
                var swap = Layers.Items[index - 1];
                Layers.Items.RemoveAt(index - 1);
                Layers.Items.Insert(index, swap);
            }
        }
    }

    private void MoveLayerDown()
    {
        if (Layers.SelectedItem != null)
        {
            int index = Layers.Items.IndexOf(Layers.SelectedItem);
            if (index < Layers.Items.Count-1)
            {
                var swap = Layers.Items[index + 1];
                Layers.Items.RemoveAt(index + 1);
                Layers.Items.Insert(index, swap);
            }
        }
    }

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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消