I am writing a datalogger application that reads live variable data from an integrated device and shows it to the user in the GUI.
I created a static class
called Database
that keeps all the variables in the LiveVariableDatabase
and have methods to add samples, add / remove variables, etc.
/ * DataBase.cs file * /
class publishes var_t
{
public chain name;
addr public UInt32;
public byte size;
Public list samples; // variable read values
};
static class database
{
Public static list LiveVariableDatabase = new list();
AddNewVariable static public bool (var_t var)
{
LiveVariableDatabase.Add (var);
// verify successfully
returns updateSucceed;
}
public static bool DeleteVariable (Var_t var)
{
LiveVariableDatabase.Remove (var);
// verify successfully
returns updateSucceed;
}
Public static addVarSample (int index, byte value)
{
LiveVariableDatabase[index].samples.Add (value);
// verify successfully
returns addSucceed;
}
}
I need to access this LiveVariableDatabase
of different classes and use the methods to make changes in the variables. I want to have only one instance of this class since all the variables will be stored in this database. So I used a static class
but I am a beginner in DO#
and I usually use global variables for this type of requirement in do
. So I could not be sure to define everything as static
It is a good form of implementation. Because I want to have a single instance of Database
but if I declare this class as static
I do not need to create an instance of that, I just call the methods and the variables in different ways like:
Is:
/ * WatchWindowForm.cs file * /
private void WatchWindowForm_FormClosing (sender object, FormClosingEventArgs e)
{
for (int i = 0; i < watchTable.RowCount; i++)
{
string varName = watchTable.GetControlFromPosition(0, i).Text;
Var_t watchVar = DataBase.LiveVariableDatabase.Find(x => x.name == varName);
DataBase.DeleteVariable (watchVar);
}
}
And the other,
/ * ScopeWindowForm.cs file * /
// Add a variable
private void bt_addVar_Click (sender object, EventArgs e)
{
// ALL: or isimde var zaten varsa!
foreach (Serie s in chart1.Series)
{
if (s.Name == tb_varName.Text)
{
MessageBox.Show ("You already have this variable added to the database!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
he came back;
}
}
Var_t var = new Var_t ();
var.addr = Convert.ToUInt32 (tb_varAddr.Text, 16);
var.size = Convert.ToByte (tb_varSize.Text, 10);
var.name = tb_varName.Text;
DataBase.AddVariable (var);
}
This is a good way to use static class
in this type of need?