troubleshooting Question

How to do a search in and sort a multidimensional List of objects in C#?

Avatar of ruud00000
ruud00000 asked on
.NET ProgrammingC#
10 Comments2 Solutions644 ViewsLast Modified:
(C#, Compact Framework 3.5, Visual Studio 2008 Professional)

I want to save the values in a form into an array until I'm finished adding values and then commit the values in the array to the database at once. I don't want to make use of a table adapter, gridview etc due to limited resources (pda), speed, need for customisation of dialogs etc.

Like this, where I first execute opslaanButton on adding each new item to the array and then execute commitButton to write it to the database.
 
private void centrumOpslaanButtton_Click(object sender, EventArgs e)
        {
            centra[itemTeller] = new Centrum_oud();
            centra[itemTeller].centrumId = int.Parse(centrumIdTextBox.Text);
            centra[itemTeller].naam = centrumNaamTextBox.Text;
            centra[itemTeller].plaats = centrumPlaatsTextBox.Text;
            centra[itemTeller].banen = int.Parse(centrumBanenTextBox.Text);
            itemTeller += 1;
            positie = itemTeller - 1;
        }

        private void centrumCommitButton_Click(object sender, EventArgs e)
        {
            using (SqlCeConnection connection = new SqlCeConnection(connectString))
            {
                connection.Open();

                // Start a local transaction.
                SqlCeTransaction sqlTran = connection.BeginTransaction();

                // Enlist the command in the current transaction.
                SqlCeCommand command = connection.CreateCommand();
                command.Transaction = sqlTran;

                try
                {
                    int rowsaffected = 0;
                    for (int i = 0; i < centra.Length; i += 1)
                    {
                        if (centra[i] != null)
                        {
                            command.CommandText = "INSERT INTO Centrum (centrumId, naam, plaats, banen) VALUES ";
                            command.CommandText += "(" + centra[i].centrumId + ", N'" + centra[i].naam + "', N'" + centra[i].plaats + "', " + centra[i].banen + ");\n";
                            rowsaffected += command.ExecuteNonQuery();
                        }
                    }

                    sqlTran.Commit();
                    centrumToonTextBox.Text = rowsaffected + " records zijn in de database opgeslagen\r\n";
                    for (int i = 0; i < centra.Length; i += 1)
                    {
                        centra[i] = null;
                    }
                    itemTeller = 0;
                    positie = - 1;
                    centrumIdTextBox.Text = "";
                    centrumNaamTextBox.Text = "";
                    centrumPlaatsTextBox.Text = "";
                    centrumBanenTextBox.Text = "";
                }
                catch (Exception ex)
                {
                    centrumToonTextBox.Text = ex.Message + "\r\nDe records zijn niet in de database opgeslagen\r\n";
                    sqlTran.Rollback();
                }
            }
        }

Open in new window


Now I want to generalize this piece of code, so I have to read the contents (.Text property) of all controls of type textBox in the form and match them to the appropriate fields in the database table. My idea was to create a List of Fields where Field is a class that contains the properties Tabindex, Name, Text (and others) like this, since the values contained are of different types (int and string) so cannot be put in an array.
 
public class Field
    {
        string naam;
        string type;
        int length;
        int tabIndex;
        string text;

        public Field(string naam, string type, int length, int tabIndex, string text) 
        {
            this.naam = naam;
            this.type = type;
            this.length = length;
            this.tabIndex = tabIndex;
            this.text = text;
        }

        public Field(string naam, string text, int tabIndex)
        {
            this.naam = naam;
            this.tabIndex = tabIndex;
            this.text = text;
        }

        public string Naam
        {
            get { return naam; }
            set { naam = value; }
        }

        public string Type
        {
            get { return type; }
            set { type = value; }
        }

        public int Length
        {
            get { return length; }
            set { length = value; }
        }

        public int TabIndex
        {
            get { return length; }
            set { length = value; }
        }

        public string Text
        {
            get { return type; }
            set { type = value; }
        }
    }

Open in new window


Thereby I would like to sort that list on property TabIndex, which must be possible using a class that implements the IComparer interface. And I want to search the list for a Field object who's name property value matches the name of the databasetablefield who's value i want to update (record by record) and this search should return the value of the Text property...

So now I have a list of FormControl attributes, filled as follows (in a loop):
 
panel2Fields.Add(new Field(this.panel2.Controls[i].Name, this.panel2.Controls[i].Text, this.panel2.Controls[i].TabIndex));

Open in new window


And need to write a comparer class and call that, to sort on TabIndex and need a way to do a search for i.e. an abject who's Name property value is centrumPlaatsTextBoxDb when I need the Text property's value of field Plaats (so I need to do a search for a Name that contains the substring 'plaats').

How is this done?
ASKER CERTIFIED SOLUTION
dimaj

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 2 Answers and 10 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 10 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros