Use of static classes for having only one instance [on hold]











up vote
-1
down vote

favorite












I'm writing a data logger application that reads live variable data from an embedded device and shows them to the user in the GUI.



I created a static class named DataBase that keeps all the variables in the LiveVariableDatabase and have methods to add samples, add/delete variables etc.



/* DataBase.cs file */

public class Var_t
{
public string name;
public UInt32 addr;
public byte size;
public List<byte> samples; //variable read values
};

static class DataBase
{
public static List<Var_t> LiveVariableDatabase = new List<Var_t>();

public static bool AddNewVariable(Var_t var)
{
LiveVariableDatabase.Add(var);

//check successfull
return updateSucceed;
}

public static bool DeleteVariable(Var_t var)
{
LiveVariableDatabase.Remove(var);

//check successfull
return updateSucceed;
}

public static bool AddVarSample(int index, byte value)
{
LiveVariableDatabase[index].samples.Add(value);

//check successfull
return addSucceed;
}
}


I need to access this LiveVariableDatabase from different classes and use the methods to make changes in the variables. I want to have only one instance of this class since all of the variables will be stored in this database. So I used a static class but I'm a beginner in C# and I normally use global variables for this kind of requirement in C. So I couldn't be sure defining everything as static is a good way of implementation. Because I want to have only one instance of DataBase but if I declare this class as static I don't need to create an instance of it, I just call the methods and the variables in different forms like:



This:



/* WatchWindowForm.cs file */

private void WatchWindowForm_FormClosing(object sender, 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(object sender, EventArgs e)
{
//TODO: o isimde var zaten varsa!
foreach (Series 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);
return;
}
}

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);
}


Is this a good way to use static class in this kind of necessity?










share|improve this question









New contributor




abdullah cinar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











put on hold as off-topic by t3chb0t, Toby Speight, Quill, 200_success, яүυк yesterday


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Toby Speight, Quill, 200_success, яүυк

If this question can be reworded to fit the rules in the help center, please edit the question.









  • 2




    You need to add the real code. This is not valid C# so it currently cannot be reviewed. There also many parts that are missing.
    – t3chb0t
    2 days ago












  • I'm new in Code Review. What do you mean by the real code? I clipped the irrelevant parts to make it simpler. Do you mean I need to add the methods that I called this class in?
    – abdullah cinar
    2 days ago






  • 1




    Yes, without code there is no Code Review ;-) so please post what you have without editing it. Just copy/paste it.
    – t3chb0t
    2 days ago










  • I copied the methods where I used this static class
    – abdullah cinar
    2 days ago










  • There are still several things missing here that cannot easily be determined from the surrounding context, and 'variable' is quite a generic term so that isn't helping. But either way, statics/globals are often a bad idea - for various reasons they tend to result in code that's difficult to maintain. If a form needs access to the variables database, then just provide it with a reference to a shared Database instance.
    – Pieter Witvoet
    yesterday















up vote
-1
down vote

favorite












I'm writing a data logger application that reads live variable data from an embedded device and shows them to the user in the GUI.



I created a static class named DataBase that keeps all the variables in the LiveVariableDatabase and have methods to add samples, add/delete variables etc.



/* DataBase.cs file */

public class Var_t
{
public string name;
public UInt32 addr;
public byte size;
public List<byte> samples; //variable read values
};

static class DataBase
{
public static List<Var_t> LiveVariableDatabase = new List<Var_t>();

public static bool AddNewVariable(Var_t var)
{
LiveVariableDatabase.Add(var);

//check successfull
return updateSucceed;
}

public static bool DeleteVariable(Var_t var)
{
LiveVariableDatabase.Remove(var);

//check successfull
return updateSucceed;
}

public static bool AddVarSample(int index, byte value)
{
LiveVariableDatabase[index].samples.Add(value);

//check successfull
return addSucceed;
}
}


I need to access this LiveVariableDatabase from different classes and use the methods to make changes in the variables. I want to have only one instance of this class since all of the variables will be stored in this database. So I used a static class but I'm a beginner in C# and I normally use global variables for this kind of requirement in C. So I couldn't be sure defining everything as static is a good way of implementation. Because I want to have only one instance of DataBase but if I declare this class as static I don't need to create an instance of it, I just call the methods and the variables in different forms like:



This:



/* WatchWindowForm.cs file */

private void WatchWindowForm_FormClosing(object sender, 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(object sender, EventArgs e)
{
//TODO: o isimde var zaten varsa!
foreach (Series 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);
return;
}
}

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);
}


Is this a good way to use static class in this kind of necessity?










share|improve this question









New contributor




abdullah cinar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











put on hold as off-topic by t3chb0t, Toby Speight, Quill, 200_success, яүυк yesterday


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Toby Speight, Quill, 200_success, яүυк

If this question can be reworded to fit the rules in the help center, please edit the question.









  • 2




    You need to add the real code. This is not valid C# so it currently cannot be reviewed. There also many parts that are missing.
    – t3chb0t
    2 days ago












  • I'm new in Code Review. What do you mean by the real code? I clipped the irrelevant parts to make it simpler. Do you mean I need to add the methods that I called this class in?
    – abdullah cinar
    2 days ago






  • 1




    Yes, without code there is no Code Review ;-) so please post what you have without editing it. Just copy/paste it.
    – t3chb0t
    2 days ago










  • I copied the methods where I used this static class
    – abdullah cinar
    2 days ago










  • There are still several things missing here that cannot easily be determined from the surrounding context, and 'variable' is quite a generic term so that isn't helping. But either way, statics/globals are often a bad idea - for various reasons they tend to result in code that's difficult to maintain. If a form needs access to the variables database, then just provide it with a reference to a shared Database instance.
    – Pieter Witvoet
    yesterday













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I'm writing a data logger application that reads live variable data from an embedded device and shows them to the user in the GUI.



I created a static class named DataBase that keeps all the variables in the LiveVariableDatabase and have methods to add samples, add/delete variables etc.



/* DataBase.cs file */

public class Var_t
{
public string name;
public UInt32 addr;
public byte size;
public List<byte> samples; //variable read values
};

static class DataBase
{
public static List<Var_t> LiveVariableDatabase = new List<Var_t>();

public static bool AddNewVariable(Var_t var)
{
LiveVariableDatabase.Add(var);

//check successfull
return updateSucceed;
}

public static bool DeleteVariable(Var_t var)
{
LiveVariableDatabase.Remove(var);

//check successfull
return updateSucceed;
}

public static bool AddVarSample(int index, byte value)
{
LiveVariableDatabase[index].samples.Add(value);

//check successfull
return addSucceed;
}
}


I need to access this LiveVariableDatabase from different classes and use the methods to make changes in the variables. I want to have only one instance of this class since all of the variables will be stored in this database. So I used a static class but I'm a beginner in C# and I normally use global variables for this kind of requirement in C. So I couldn't be sure defining everything as static is a good way of implementation. Because I want to have only one instance of DataBase but if I declare this class as static I don't need to create an instance of it, I just call the methods and the variables in different forms like:



This:



/* WatchWindowForm.cs file */

private void WatchWindowForm_FormClosing(object sender, 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(object sender, EventArgs e)
{
//TODO: o isimde var zaten varsa!
foreach (Series 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);
return;
}
}

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);
}


Is this a good way to use static class in this kind of necessity?










share|improve this question









New contributor




abdullah cinar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I'm writing a data logger application that reads live variable data from an embedded device and shows them to the user in the GUI.



I created a static class named DataBase that keeps all the variables in the LiveVariableDatabase and have methods to add samples, add/delete variables etc.



/* DataBase.cs file */

public class Var_t
{
public string name;
public UInt32 addr;
public byte size;
public List<byte> samples; //variable read values
};

static class DataBase
{
public static List<Var_t> LiveVariableDatabase = new List<Var_t>();

public static bool AddNewVariable(Var_t var)
{
LiveVariableDatabase.Add(var);

//check successfull
return updateSucceed;
}

public static bool DeleteVariable(Var_t var)
{
LiveVariableDatabase.Remove(var);

//check successfull
return updateSucceed;
}

public static bool AddVarSample(int index, byte value)
{
LiveVariableDatabase[index].samples.Add(value);

//check successfull
return addSucceed;
}
}


I need to access this LiveVariableDatabase from different classes and use the methods to make changes in the variables. I want to have only one instance of this class since all of the variables will be stored in this database. So I used a static class but I'm a beginner in C# and I normally use global variables for this kind of requirement in C. So I couldn't be sure defining everything as static is a good way of implementation. Because I want to have only one instance of DataBase but if I declare this class as static I don't need to create an instance of it, I just call the methods and the variables in different forms like:



This:



/* WatchWindowForm.cs file */

private void WatchWindowForm_FormClosing(object sender, 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(object sender, EventArgs e)
{
//TODO: o isimde var zaten varsa!
foreach (Series 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);
return;
}
}

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);
}


Is this a good way to use static class in this kind of necessity?







c# object-oriented static






share|improve this question









New contributor




abdullah cinar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




abdullah cinar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 2 days ago





















New contributor




abdullah cinar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 days ago









abdullah cinar

11




11




New contributor




abdullah cinar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





abdullah cinar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






abdullah cinar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




put on hold as off-topic by t3chb0t, Toby Speight, Quill, 200_success, яүυк yesterday


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Toby Speight, Quill, 200_success, яүυк

If this question can be reworded to fit the rules in the help center, please edit the question.




put on hold as off-topic by t3chb0t, Toby Speight, Quill, 200_success, яүυк yesterday


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Toby Speight, Quill, 200_success, яүυк

If this question can be reworded to fit the rules in the help center, please edit the question.








  • 2




    You need to add the real code. This is not valid C# so it currently cannot be reviewed. There also many parts that are missing.
    – t3chb0t
    2 days ago












  • I'm new in Code Review. What do you mean by the real code? I clipped the irrelevant parts to make it simpler. Do you mean I need to add the methods that I called this class in?
    – abdullah cinar
    2 days ago






  • 1




    Yes, without code there is no Code Review ;-) so please post what you have without editing it. Just copy/paste it.
    – t3chb0t
    2 days ago










  • I copied the methods where I used this static class
    – abdullah cinar
    2 days ago










  • There are still several things missing here that cannot easily be determined from the surrounding context, and 'variable' is quite a generic term so that isn't helping. But either way, statics/globals are often a bad idea - for various reasons they tend to result in code that's difficult to maintain. If a form needs access to the variables database, then just provide it with a reference to a shared Database instance.
    – Pieter Witvoet
    yesterday














  • 2




    You need to add the real code. This is not valid C# so it currently cannot be reviewed. There also many parts that are missing.
    – t3chb0t
    2 days ago












  • I'm new in Code Review. What do you mean by the real code? I clipped the irrelevant parts to make it simpler. Do you mean I need to add the methods that I called this class in?
    – abdullah cinar
    2 days ago






  • 1




    Yes, without code there is no Code Review ;-) so please post what you have without editing it. Just copy/paste it.
    – t3chb0t
    2 days ago










  • I copied the methods where I used this static class
    – abdullah cinar
    2 days ago










  • There are still several things missing here that cannot easily be determined from the surrounding context, and 'variable' is quite a generic term so that isn't helping. But either way, statics/globals are often a bad idea - for various reasons they tend to result in code that's difficult to maintain. If a form needs access to the variables database, then just provide it with a reference to a shared Database instance.
    – Pieter Witvoet
    yesterday








2




2




You need to add the real code. This is not valid C# so it currently cannot be reviewed. There also many parts that are missing.
– t3chb0t
2 days ago






You need to add the real code. This is not valid C# so it currently cannot be reviewed. There also many parts that are missing.
– t3chb0t
2 days ago














I'm new in Code Review. What do you mean by the real code? I clipped the irrelevant parts to make it simpler. Do you mean I need to add the methods that I called this class in?
– abdullah cinar
2 days ago




I'm new in Code Review. What do you mean by the real code? I clipped the irrelevant parts to make it simpler. Do you mean I need to add the methods that I called this class in?
– abdullah cinar
2 days ago




1




1




Yes, without code there is no Code Review ;-) so please post what you have without editing it. Just copy/paste it.
– t3chb0t
2 days ago




Yes, without code there is no Code Review ;-) so please post what you have without editing it. Just copy/paste it.
– t3chb0t
2 days ago












I copied the methods where I used this static class
– abdullah cinar
2 days ago




I copied the methods where I used this static class
– abdullah cinar
2 days ago












There are still several things missing here that cannot easily be determined from the surrounding context, and 'variable' is quite a generic term so that isn't helping. But either way, statics/globals are often a bad idea - for various reasons they tend to result in code that's difficult to maintain. If a form needs access to the variables database, then just provide it with a reference to a shared Database instance.
– Pieter Witvoet
yesterday




There are still several things missing here that cannot easily be determined from the surrounding context, and 'variable' is quite a generic term so that isn't helping. But either way, statics/globals are often a bad idea - for various reasons they tend to result in code that's difficult to maintain. If a form needs access to the variables database, then just provide it with a reference to a shared Database instance.
– Pieter Witvoet
yesterday















active

oldest

votes






















active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

Quarter-circle Tiles

build a pushdown automaton that recognizes the reverse language of a given pushdown automaton?

Mont Emei