Ok... I'm trying to understand this whole object oriented programming but I keep ending up in a dead end. ;)
I'm trying to store or rather will store a great deal of data with using classes as I should. I want to store planetary data by using a class with several properties, I will then save these into a list.
My problem is that I don't know how to make this list globally accessible, it is only accessible in the instance where it is created.
Some sample code from my test environment below.
OrbitalBodies.cs
class OrbitalBodies
{
public int BodyID { get; set; }
public string BodyName { get; set; }
public int BodySize { get; set; }
public int BodyX { get; set; }
public int BodyY { get; set; }
}
From1.cs
public void button1_Click(object sender, EventArgs e)
{
var bodies0 = new OrbitalBodies();
var orbitalList = new List<OrbitalBodies>();
bodies0.BodyID = 4;
bodies0.BodyName = "Earth";
bodies0.BodySize = 125;
bodies0.BodyX = -450;
bodies0.BodyY = 75;
orbitalList.Add(bodies0);
bodies0.BodyID = 0;
bodies0.BodyName = "Sol";
bodies0.BodySize = 500;
bodies0.BodyX = 0;
bodies0.BodyY = 0;
orbitalList.Add(bodies0);
//var orbitalDic = new Dictionary<int, OrbitalBodies>();
MessageBox.Show("Planetary body name: " + Convert.ToString(orbitalList.Count()));
}
I have spent a couple of hours looking up my problem here and other places but I don't know how I can access the information I put into the list other than in that single instance. My real application will have tens of thousands of orbital bodies and many other data that must be accessible throughout many forms and perhaps even other classes.
Some help would be appreciated, what is the best solution? Do it completely differently?!?
You don't want static members or singletons (both of which cause more problems than they solve), you need Dependency Injection.
Outside of your form create the List, pass it into the forms constructor. Everywhere you need to use the list, pass the instance you have from the form.
This way there is only one list, everywhere that needs the list is passed a list (that just happens to be the same list).
If in time you realize you actually need to model two different systems, you just create two different lists, and pass them to two different forms, everything keeps working and you don't need to go back through your code removing references to static members.
Honestly, this is completely doable without going to the dark side and perpetuating the evil that is static/global variables.
NB Why static variables are considered evil
Use design pattern Singleton.
public class Globals
{
private List<OrbitalBodies>() orbiralList;
private static Globals instance;
private Globals()
{
this.orbiralList = new List<OrbitalBodies>();
this.instance = NULL;
}
public static List<OrbitalBodies>() GetOrbitalBodies()
{
if (instance == null) instance = new Globals();
return instance.orbitaList;
}
}
Then everywhere in your code, when you will need orbitalList
call
Globals.GetOrbitalBodies().<do whatever with your list>
Try not to use static classes, because they are mess in term of OO design.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With