Reputation: 51
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?!?
Upvotes: 4
Views: 14280
Reputation: 51719
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
Upvotes: 3
Reputation: 1469
If you just want to access your list within the "Form1" class, just declare it as a private member out of a function:
private List<OrbitalBodies> _orbitalList;
and then instanciate it into your "button1_Click" method.
If you want to access your list in all your classes, I suggest you make it static :
public class NewClass
{
public static List<OrbitalBodies> OrbitalList {get; set;};
}
and you call it like this
NewClass.OrbitalList;
Hope that helps.
Upvotes: 0
Reputation: 9527
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.
Upvotes: 1
Reputation: 10427
Make OrbitalList
a property:
public List<OrbitalBodies> OrbitalList {get;set;}
public void button1_Click(object sender, EventArgs e)
{
var bodies0 = new OrbitalBodies();
bodies0.BodyID = 4;
bodies0.BodyName = "Earth";
bodies0.BodySize = 125;
bodies0.BodyX = -450;
bodies0.BodyY = 75;
OrbitalList.Add(bodies0);
//...
}
//Then you can do:
doSomething(myForm.OrbitalList[0]);
Upvotes: 0