?
Solved

How to build a list of elements in C#?

Posted on 2007-10-02
4
Medium Priority
?
1,677 Views
Last Modified: 2008-01-09
Hello experts,

  I am working with visual studio C# 2005 so I have access to the .NET framework.
I need a data structure (probably a List, but I am opened to any suggestions) in which there will be many elements (most of the time between 1 and 10). It is not necessarily ordered.

  The elements have many fields. By instance it could be:

MyElementClass
{
  int sequenceNb;
  string toto = new string('firstElement');
}

 I need to find an element in the List by using ONLY its sequenceNb NOT its string value.
I will need to remove an element no matter of its location in the List.

  Which class so you suggest (List, Collection, ...)? Can you show me a code snippet?
It is not for an homework.

Regards,

Pascal
0
Comment
Question by:pascal_lalonde
4 Comments
 
LVL 12

Expert Comment

by:RubenvdLinden
ID: 19999011
A very simple example to store an element in an arraylist and retrieving it using the Find method:

public partial class Form1 : Form
    {
        System.Collections.ArrayList a = new System.Collections.ArrayList();
       
        public Form1()
        {
            InitializeComponent();

            a.Add(new MyElementClass(1234, "Test"));

            MyElementClass m = Find(1234);
            MessageBox.Show(m.SequenceNumber.ToString());
        }

        public MyElementClass Find(int seqNumber)
        {
            foreach (MyElementClass m in a)
            {
                if (m.SequenceNumber == 1234)
                {
                    return m;
                }
            }

            return null;
        }
    }


    public class MyElementClass
    {
        int sequenceNb;
        string toto;

        public MyElementClass(int sqNumber, string name)
        {
            sequenceNb = sqNumber;
            toto = name;
        }

        public int SequenceNumber
        {
            get
            {
                return sequenceNb;
            }
        }
    }
0
 
LVL 10

Expert Comment

by:dkloeck
ID: 19999021
if you want only to insert MyElementClass type objects use
List<MyElementClass> myArray = new List<MyElementClass>();

it is slightly faster that ArrayList since there is no boxing-unboxing
0
 
LVL 2

Accepted Solution

by:
hdkelly earned 500 total points
ID: 19999081
If the sequenceNb is unique you could use a dictionary:

1. Declare
Dictionary<int, MyElementClass> elementLookup = new Dictionary<int, MyElementClass>();

2. Add elements
elementLookup.Add(sequenceNbValue, elementInstance);

3. Retrieve elements by sequenceNb value
MyElementClass retrievedElement = elementLookup[sequenceNbValue];
0
 

Author Comment

by:pascal_lalonde
ID: 20000591
Wow! I like when I don't have to reinvent the wheel. Thank you!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Integration Management Part 2
Loops Section Overview
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question