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?
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?
– ptkato
Set this Array as Static I believe solves
– user6026
In the class itself
ClassDatabase
?– ptkato
I’ll add an addendum to this example, Wait ...
– user6026
Okay, just use the second option
– user6026
Could you explain to me why I can only call the class methods
ClassDatabase
using the class name itself instead of an instance?– ptkato
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
– user6026