As the titles says, I need to implement an array of singleton class.
My project idea is building a simple PLC-Ladder IDE written in Microsoft Visual C#.
Because I'm not an expert code writer, I wrote a large proportion of my code in a single file. It worked properly, but it was not easy to read and update. So I split it in five different classes.
MainClass.cs - Main program
CustomMethods.cs - All my methods
CustomEventHandlers.cs - All my event handlers
Components.cs - This class holds information (name, type, input, output, etc) and I need a 2-D array of this, because I'm using it as a "screen buffer" for the project.
SharedVariables.cs - All my variables are declared here. My components class is initialized as a 2-D array in here too.
The problem is that I cannot create and share an instance of "Components" array between my classes. I found the "Singleton pattern", but still I cannot implement it properly. I can't initialize my array of "singleton" class. Here is the code (irrelevant parts are cut out).
//Custom method.cs
namespace AVR_PLC
{
class CustomMethods
{
CustomEventHandlers extHandlers;
SharedVariables Variables;
public void setup()
{
extHandlers = new CustomEventHandlers();
Variables = new SharedVariables();
for (int i = 0; i < Variables.maxX; i++)
for (int j = 0; j < Variables.maxY; j++)
Variables.pBuffer[i, j] = new Components;
}
---------------------------------------------
//SharedVariables.cs
namespace AVR_PLC
{
class SharedVariables
{
public int maxX = 10;
public int maxY = 100;
public Components[,] pBuffer = Components[10, 100]; //Panel buffer
}
}
---------------------------------------------
//Components.cs
namespace AVR_PLC
{
public class Components
{
//***** Singleton pattern implementation ***********
//Private static object.
private static volatile Components instance;
private static readonly object mutex = new Object();
//Private constructor to prevent object creation.
private Components()
{
}
//Public property to access outside of the class to create an object.
public static Components Instance
{
get
{
if (instance == null)
lock (mutex)
if (instance == null)
instance = new Components();
return instance;
}
}
//************************************************
private string _name = "Default name";
public string Name
{
get { return this._name; }
set { this._name = value; }
}
public void reset()
{
_name = "Default name";
}
}
}
A singleton is a class which there is only one instance of (Personally, when using C# I prefer static classes instead of singletons).
To access the singleton class you use the Components.Instance
property. BUT, in your case, you dont actually want a singleton.
If you want an array of Component objects, you would rather want to create a new one for each new object in the array, else the array is kinda useless (if its exactly the same object in it...).
To share a variable between multiple instances of the same class you use the static
keyword, which basically makes it the same object over all classes (if you change it in one, it changes in all).
So you could just make your shared variables class a static class:
public static class SharedVariables
{
public static int maxX = 10;
public static int maxY = 100;
public static Components[,] pBuffer = Components[10, 100];
}
And they will be reachable wherever you access the SharedVariables class.
The big questions is though: Do you really need a static SharedVariables
class? I kinda suspect you got a design issue here to think about.