Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8187269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:30:19+00:00 2026-06-07T02:30:19+00:00

Possible Duplicate: Swap two items in List<T> Edit: Maybe this will work for getting

  • 0

Possible Duplicate:
Swap two items in List<T>

Edit: Maybe this will work for getting the ‘b’ value?

for (int i = 0; i < inventory.Count; i++)
{
    if (inventory[a].ItemRectangle.Intersects(inventory[i].ItemRectangle))
    {
        itemB = inventory[i];
    }
}

Edit: Here’s my progress.

Item itemA;
Item itemB;

int a = -1;
int b = -1;

if (a != -1 && b != -1)
{
    itemA = inventory[a];
    itemB = inventory[b];

    Swap(ref itemA, ref itemB);

    inventory[a] = itemA;
    inventory[b] = itemB;
}

And here’s is where I’m getting the ‘a’ value.

if (item.ItemSelected == true)
{
    a = item.ItemIndex;
}
else
    a = -1;

I haven’t figured out how to get the ‘b’ value because I would have to check for an item colliding with another item that are both in the same list. If anybody know how I can do this, please tell me. It would look something like this I guess:

if (item.ItemRectangle.Intersects(//the other item.ItemRectangle)
{
    b = item.ItemIndex;
}
else
    b = -1;

I’ve made a List < Item > called inventory. So now I want to implement a swap function, like this:

foreach (Item item in inventory)
{
    if (mouseRectangle.Intersects(item.ItemRectangle))
    {
        if (Input.EdgeDetectLeftMouseDown())
        {
            switch (item.ItemSelected)
            {
                case false:
                    item.ItemSelected = true;
                    break;
                case true:
                    item.ItemSelected = false;
                    break;
            }
        }  
    }
    else if (Input.EdgeDetectLeftMouseDown())
    {
        switch (item.ItemSelected)
        {
            case true:
                item.ItemSelected = false;
                break;
        }
    }
    else if (item.ItemSelected == true)
    {
        item.ItemPosition = new Vector2(mouseRectangle.X, mouseRectangle.Y);
        item.ItemRectangle = new Rectangle(mouseRectangle.X, mouseRectangle.Y, 32, 32);
    }
    else if (item.ItemSelected == false && //a lot of checks to determine it is not intersecting with an equip slot
    {
        item.ItemPosition = item.OriginItemPosition;
        item.ItemRectangle = item.OriginItemRectangle;
    }
    else if (item.ItemRectangle.Intersects(item.ItemRectangle))
    {
        //SwapItem(inventory, item, item);
    }

So that’s the part of the code I need help with. I want any item in the list to be able to swap with any other item in the list. My SwapItem method is just a placeholder, I dont actually have a SwapItem method yet.

I want the arguments that you pass in to the method to be related to the items I want to swap. So the first item would be the item that I have selected with my mouse, and the other item should be the item that the first item is intersecting with.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T02:30:21+00:00Added an answer on June 7, 2026 at 2:30 am

    To swap the values of two variables, the easiest method is using references. This is a classic pointer exercise in c++, but it can apply to C# as well.

    // Replace int with any data type / class you need
    void Swap (ref int a, ref int b)
    {
       int c = a; 
       a = b; 
       b = c;
    }
    

    The algorithm used is very simple, and the explanation is usually done like this: you have two glasses, one with water, and one with oil. To put the oil in the first glass, you will need to use a third glass, put the water inside, then put the oil in the first glass, and the water in the second one.


    Here is what I had in mind. Look for the comments, so you can understand what’s going on.:

    // Unlike foreach, with for I can change the values in the list
    for (int i = 0; i < inventory.Count; i++)
    {
        if (mouseRectangle.Intersects(inventory[i].ItemRectangle))
        {
            if (Input.EdgeDetectLeftMouseDown())
            {
                // You can replace the switch with this shorter structure
                // if A is a bool value, !A will have the opposite value
                inventory[i].ItemSelected = !inventory[i].ItemSelected;
            }  
        }
        else if (Input.EdgeDetectLeftMouseDown())
        {
            // You don't need a case-switch for a single condition. An if should suffice
            if (inventory[i].ItemSelected) 
                inventory[i].ItemSelected = false;
        }
        else if (inventory[i].ItemSelected == true)
        {
            inventory[i].ItemPosition = new Vector2(mouseRectangle.X, mouseRectangle.Y);
            inventory[i].ItemRectangle = new Rectangle(mouseRectangle.X, mouseRectangle.Y, 32, 32);
        }
        else if (inventory[i].ItemSelected == false && //a lot of checks to determine it is not intersecting with an equip slot
        {
            inventory[i].ItemPosition = inventory[i].OriginItemPosition;
            inventory[i].ItemRectangle = inventory[i].OriginItemRectangle;
        }
    
        // Something definitely wrong with this line, a rectangle to instersect with itself??
        else if (inventory[i].ItemRectangle.Intersects(inventory[PROBABLY_SOMETHING_ELSE].ItemRectangle))
        {
            Swap (ref inventory[i], ref inventory[PROBABLY_SOMETHING_ELSE])
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How does XOR variable swapping work? Swap the values of two variables
Possible Duplicate: Swapping two variable value without using 3rd variable we have int a=4;
Possible Duplicate: Is it possible to write swap method in Java? Given two values
Possible Duplicate: How do I calculate someone's age in C#? Maybe this could be
Possible Duplicate: Javascript swap array elements I have a array like this: this.myArray =
Possible Duplicate: array_splice() for associative arrays How to add an array value to the
Possible Duplicate: thread with multiple parameters How does one thread a sub with two
Possible Duplicate: How can I combine multiple rows into a comma-delimited list in Oracle?
Possible Duplicate: php == vs === operator Reference - What does this symbol mean
(NOT a possible duplicate of Swap text around equal sign :) Very often I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.