How to create an arrays Arraylist?

Asked

Viewed 1,354 times

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 supposed to happen there is a record, then a validation.

The user registers and presses the button to confirm, the information is sent to the method RegDatabase class ClassDatabase, adding that information to the type list string[], InfoList. When the user tries to enter, he puts the information in the Textbox and click the button, this sends the information to the other method of the class ClassDatabase, the InDatabase, this method takes the list InfoList and opens each array by comparing the data of each array within the list InfoList with the data entered by the user; if the condition of the if, the method returns true and enters the system, if not, 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, as I could do the above?

Is there any more practical way to make this record/validation (DLL, for example)? If so, which?

1 answer

3


I noticed that your class presented some coding errors, I made the changes on your code and it was 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

inserir a descrição da imagem aqui

Note: Notice that it is working and he found the value inside the List<String[]> !!!


2) Option

Another way with data persistence until program closure

Code

public static class ClassDatabase
{
    static ClassDatabase()
    {
        ClassDatabase.InfoList = new List<string[]>();
    }
    public static List<string[]> InfoList { get; private set; }
    public static void RegDatabase(String A, String B)
    {
        InfoList.Add(new String[2] { A, B });
    }
    public static 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][3]))
                {
                    ret = true;
                }
            }
            i++;
        }
        return ret;
    }
}

How to use

Form1 will be responsible for filling it the first time.

private void Form1_Load(object sender, EventArgs e)
{            
       //ADICIONANDO VALORES
       ClassDatabase.RegDatabase("Valor1", "Valor2");
       ClassDatabase.RegDatabase("Valor3", "Valor4");
       ClassDatabase.RegDatabase("Valor5", "Valor6");
       ClassDatabase.RegDatabase("Valor7", "Valor8");
}

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

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

inserir a descrição da imagem aqui

Reference:

  • That’s right, but there is a problem... Suppose there is a Form3 that arises after the validation of the data. Okay, I do what I should do in this Form3, when I leave this Form3 back to Form2, the data "vanish", insert the data I registered previously and try to validate, but nothing happens, as if it was being started for the first time. What can it be?

  • 1

    Set this Array as Static I believe solves

  • In the class itself ClassDatabase?

  • I’ll add an addendum to this example, Wait ...

  • Okay, just use the second option

  • Could you explain to me why I can only call the class methods ClassDatabase using the class name itself instead of an instance?

  • 1

    When using the Static modifier the Classdatabase need not be more instances, and you can access its methods, properties directly by the name of the class as it is in the example, I put another reference that this much clearer for your understanding. In your case how you wanted to access it from any form is the way not to lose the data that was loaded at the beginning of your program

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.