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 1013411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:05:51+00:00 2026-05-16T10:05:51+00:00

I am using a List construct to handle the sequence in which images are

  • 0

I am using a List construct to handle the sequence in which images are drawn in “OnPaint”. Now if my images are re-ordered (e.g. “brought to front” or “..to back”), I’d need to reposition them in my List.
I have trouble doing so, because List doesn’t support a method similar to setIndex().

So what I am trying to do is basically:

    private List<BitmapWithProps> activeImages = new List<BitmapWithProps>();

    public void addActiveImage(BitmapWithProps image)
    {
        activeImages.Add(image);
    }

    public BitmapWithProps getActiveImage(int index)
    {
        return activeImages[index];
    }

    public void removeActiveImage(int index)
    {
        activeImages.RemoveAt(index);
    }

    public void removeActiveImage(BitmapWithProps item)
    {
        activeImages.Remove(item);
    }

    public void swapActiveImageIndex(int sourceIndex, int destIndex)
    {
        // what would the code look like in here if I were to swap
        // the 2nd item (1) with the 4th one (3) in a 5-item-List (0 - 4)
    }

I want to be able to swap an index.. sort of. I could “insert” a new item into the List at the index it should go to and assign the value and then delete the other “source”. However it doesn’t seem elegant in any way.

I’d be glad for any hints and please excuse me if I overlooked a thread – I did search though, before asking.

dS.

  • 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-05-16T10:05:52+00:00Added an answer on May 16, 2026 at 10:05 am

    What’s inelegant about doing the steps you want to do? If you want to change something’s position within a List (which is a vector style collection) then what you want to do is insert it at the new position and remove it from the old. Precisely what you are complaining about having to do.

    If it really upsets you then write an extension method:

    public static void MoveIndex<T>(this List<T> list, int srcIdx, int destIdx)
    {
      if(srcIdx != destIdx)
      {
        list.Insert(destIdx, list[srcIdx]);
        list.RemoveAt(destIdx < srcIdx ? srcIdx + 1 : srcIdx);
      }
    }
    

    Edit: Oh, you just want to swap? Simpler (and more efficient) still:

    public static void SwapItems<T>(this List<T> list, int idxX, int idxY)
    {
      if(idxX != idxY)
      {
        T tmp = list[idxX];
        list[idxX] = list[idxY];
        list[idxY] = tmp;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Sectioned List in my activity which extends ListActivity but now as
I'm making PriorityQueue<T> classes, and right now I am using List<T> as a backing
I binded a gridview using list private void BindEntirePropertyGrid() { List<Tbl_RoomMaster> items = new
I am using List in C#. Code is as mentioned below: TestCase.cs public class
I am creating a app in android. In that i am using list view.
I am using a list view and an adapter for loading a list,each list
I'm using a list of lists to store a matrix in python. I tried
In C++ using std::list , removing a member is a simple matter of erasing
I am using a list view in Android 1.5 to show a list of
I have simple DAL that consists of a SalesEnquiry object, which includes a List<T>

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.