How to create an ArrayList of arrays?

3

I'm having some problems creating a List of Arrays, I'm using Visual C #, .NET 3.5.

Code:

using Database;
//...
namespace Sample {
    public partial class Form1 : Form {
        //...
        private void Button1_Click(object sender, EventArgs e) {
            if ((TextBox1.Text).Equals("") || (TextBox2.Text).Equals("")) {
                MessageBox.Show(null, "Preencha todos os campos!", "Erro");
            } else {
                ClassDatabase Call = new ClassDatabase();
                Call.RegDatabase(TextBox1.Text, TextBox2.Text);
                //...
            }
        }
        //...
    }
}

namespace Database {
    class ClassDatabase {
        List<string[]> InfoList = new List<string[]>();
        public void RegDatabase(String A, String B) {
            string[] DataSet = new string[2];
            DataSet[0] = A;
            DataSet[1] = B;
            this.InfoList.AddRange(DataSet);
        } public bool InDatabase (String A, String B) {
            int j = InfoList.Count;
            for (int i = 0; i < j; i ++) {
                string[] DataGet = new string[2];
                DataGet = this.InfoList[i];
                if (DataGet[0].Equals(A) && DataGet[1].Equals(B)) {
                    return true;
                } else {
                    return false;
                }
            }
        }
    }
}

using Database;
//...
namespace Sample {
    public partial class Form2 : Form {
        //...
        ClassDatabase Call = new ClassDatabase();
        private void Button1_Click(object sender, EventArgs e) {
            if (Call.InDatabase(TextBox1.Text, TextBox2.Text)) {
                //...
            } else {
                MessageBox.Show(null, "Falha ao entrar.", "Erro");
            }
        }
        //...
    }
}

What was to happen there is a record, then a validation.

The user registers and presses the button to confirm, the information is sent to the RegDatabase method of the ClassDatabase class, which adds this information to the list of type string [] ,% with%. When the user tries to enter, he places the information in the TextBox and clicks the button, this sends the information to the other method of class InfoList , ClassDatabase , this method takes the InDatabase list % and opens each array, comparing the data of each array within the InfoList list with the data entered by the user; if it satisfies the if condition, the method returns true and enters the system, otherwise it returns false and this generates an error message.

Well, that's what I tried to make it happen, but it's not what happens, the problem, perhaps, is my lack of experience, but anyway, how could I do the above?

Is there any more practical way of doing this registration / validation (DLL, for example)? If so, which one?

    
asked by anonymous 23.04.2014 / 03:19

1 answer

3

I noticed that your class had some coding errors, I made changes to your code and looked like this:

1) Option

Code

    public class ClassDatabase
    {
        protected List<string[]> InfoList = new List<string[]>();
        public void RegDatabase(String A, String B)
        {
            InfoList.Add(new String[2] { A, B });
        }
        public bool InDatabase(String A, String B)
        {
            bool ret = false;            
            int i = 0;
            while (ret == false && i < InfoList.Count)
            {
                if (InfoList[i] != null)
                {
                    if (A.Equals(InfoList[i][0]) && 
                        B.Equals(InfoList[i][1]))
                    {
                        ret = true;
                    }                    
                }
                i++;
            }
            return ret;
        }
    }

How to use

ClassDatabase db = new ClassDatabase();
//ADICIONANDO VALORES
db.RegDatabase("Valor1", "Valor2");
db.RegDatabase("Valor3", "Valor4");

//VERIFICANDO SE O VALOR EXISTE
bool ret = db.InDatabase("Valor3", "Valor4");

Debugging Code

Note:NotethatitisworkingandithasfoundthevalueinsideList<String[]>!!!

2)Option

Anotherpersistentwaytoclosetheprogram

Code

publicstaticclassClassDatabase{staticClassDatabase(){ClassDatabase.InfoList=newList<string[]>();}publicstaticList<string[]>InfoList{get;privateset;}publicstaticvoidRegDatabase(StringA,StringB){InfoList.Add(newString[2]{A,B});}publicstaticboolInDatabase(StringA,StringB){boolret=false;inti=0;while(ret==false&&i<InfoList.Count){if(InfoList[i]!=null){if(A.Equals(InfoList[i][0])&&B.Equals(InfoList[i][3])){ret=true;}}i++;}returnret;}}

Howtouse

Form1willberesponsibleforthefirsttime.

privatevoidForm1_Load(objectsender,EventArgse){//ADICIONANDOVALORESClassDatabase.RegDatabase("Valor1", "Valor2");
       ClassDatabase.RegDatabase("Valor3", "Valor4");
       ClassDatabase.RegDatabase("Valor5", "Valor6");
       ClassDatabase.RegDatabase("Valor7", "Valor8");
}

Form2 (or any form) can see the values added until the program closes.

private void Form2_Load(object sender, EventArgs e)
{
   //ClassDatabase.InDatabase
   //ClassDatabase.RegDatabase
}

Reference:

23.04.2014 / 06:51