We help IT Professionals succeed at work.
Get Started

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

ruud00000
ruud00000 asked
on
641 Views
Last Modified: 2013-12-16
(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?
Comment
Watch Question
Commented:
This problem has been solved!
Unlock 2 Answers and 10 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE