4

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?!?

Filburt
  • 17,626
  • 12
  • 64
  • 115
user2411504
  • 51
  • 1
  • 1
  • 3
  • 2
    You probably don't *want* it to be global, from an application design perspective. While it seems easier at first that trying to determine what it's proper scope is and ensuring it is scoped at that level, this programming methodology turns your whole application into a bunch of spaghetti code that becomes really hard to work with, understand, and maintain over time. Also note that you'll need to deal with thread safety issues if you expose this information globally; and those can be a real pain. – Servy May 23 '13 at 14:39
  • @Servy its not entirely true. Many things, mostly in game development, uses static data to store game (world) or other things, that do not change during run or needed to be accesible from everywhere and everytime. – Martin Perry May 23 '13 at 14:42
  • @MartinPerry I wasn't say you shouldn't ever make anything globally accessible, I was simply saying that, in this context, it doesn't seem to be appropriate given what he's doing, and that it's rare to be in a situation where it's appropriate. If that wasn't clear I apologize. – Servy May 23 '13 at 14:45
  • @Martin Perry: No excuse for making it a global variable. Dependency injection is the way forward dude. – Binary Worrier May 23 '13 at 14:47
  • Well, this is the beginning of a type of game. A game that will handle quite allot of data. Not the common mainstream shooter sort of...;) I will need quite alot of data to be accessible all the time, reading from a database will only be viable during save and load. More or less... – user2411504 May 23 '13 at 15:11

4 Answers4

3

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

Community
  • 1
  • 1
Binary Worrier
  • 50,774
  • 20
  • 136
  • 184
  • As much as I agree with DI being a good solution here, that's not to say that a static variable *can't* do the job - like I say if it's managed correctly it can work. I have seen static variables used in application design in the past and used well. – James May 23 '13 at 14:55
  • 4
    Yeah, it's like saying you don't need a hammer to get a nail into some wood, you can use a glass bottle instead . . . if you manage it correctly. The correct tool for the job is still a hammer. – Binary Worrier May 23 '13 at 15:02
  • @BinaryWorrier it's more like saying there is more than one type of hammer - both solutions do job. – James May 23 '13 at 22:37
  • 1
    @james: We may agree to differ mate, I don't see global/static variables as a type of hammer. – Binary Worrier May 24 '13 at 06:10
  • @BinaryWorrier well I personally wouldn't class DI as a hammer either but it was the analogy you used haha fair enough, point being if global/static variables weren't to be used, they wouldn't exist and I think (in this particular instance) it would do the job. Multi-threaded environment however...probably not. – James May 24 '13 at 07:39
  • 1
    Goto's exist, and it's well established that they should never _(where never -> 0)_ be used e.g. [GOTO still considered harmful?](http://stackoverflow.com/questions/46586/goto-still-considered-harmful) and [screw good practice, how bad could it be](http://xkcd.com/292/). I'm at the point where I'd happily put _global variables_ in the same rocket with _goto_ and shoot them both into the sun. – Binary Worrier May 24 '13 at 11:32
  • 1
    You are condemning the tool rather than the workman here though, it's not that GOTOs/global variables are bad, it's the fact that (more often than not) they are misused. Not that I am saying use this stuff in everyday programming - just merely reiterating they *do* have a purpose can do a job **if used correctly**. – James May 24 '13 at 18:11
  • @James Ouch, looks like [you have him on the ropes](http://www.youtube.com/watch?v=0e2kaQqxmQ0) – Ruben Bartelink May 24 '13 at 19:28
  • 1
    I'll happily condemn both the tool and the work man :) lets agree to disagree mate. – Binary Worrier May 30 '13 at 19:48
1

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.

Martin Perry
  • 9,232
  • 8
  • 46
  • 114
  • No, [singletons are evil](http://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons) – Binary Worrier May 23 '13 at 14:43
  • Better than static classes. And for many things, they are good solution. Not in this case though, but for beginner, if he wants "fast" to see some result, they are enough. – Martin Perry May 23 '13 at 14:45
  • @MartinPerry It's not really any different than a static class at all, at least as it is. About the only real difference is that a singleton can implement an interface and leverage polymorphism, but you aren't doing that, so it's not really any better off. – Servy May 23 '13 at 14:47
  • No, not better, static classes are just as evil, and I couldn't disagree more mate, there is a vanishingly small set of problems for which a global variable is a "good solution". In time it will bite you in the ass. Better to take the pain up front and do it right the first time. – Binary Worrier May 23 '13 at 14:48
  • @Servy I think that in memory static class is somewhere else. Singleton class is stored as any other class, only its instance is placed elsewhere. – Martin Perry May 23 '13 at 14:49
  • 1
    @MartinPerry And why does it matter that there is a static reference to an object with a reference to a list instead of just a static reference to a List? What difference (in this context) does it make? – Servy May 23 '13 at 14:51
  • @MartinPerry: Where it's stored doesn't matter, you're still accessing it through a single static globally available location. It's just as bad mate, sorry. – Binary Worrier May 23 '13 at 14:54
  • OK :) Dependency Injection looks like cleaner solution, though I never used them – Martin Perry May 23 '13 at 14:56
  • 1
    @MartinPerry I'm sure you have. You may not have called it that, but you have. – Servy May 23 '13 at 15:05
  • Exactly what do you all propose to do when most of those values will be used continuously through the game. Planetary bodies will change perhaps every five to thirty seconds, depending on which pace the player choose to act on? – user2411504 May 23 '13 at 15:16
0

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]);
Ahmed KRAIEM
  • 10,267
  • 4
  • 30
  • 33
0

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.

orel
  • 1,384
  • 10
  • 12
  • No, [static variables are evil](http://stackoverflow.com/questions/7026507/why-are-static-variables-considered-evil) – Binary Worrier May 23 '13 at 14:42
  • Ok, I learned something today. But you didn't have to downvote my answer, as I was trying to help. – orel May 23 '13 at 14:52
  • Sorry bud but I didn't downvote your answer. I wouldn't downvote answers on a question where I've also provided an answer. – Binary Worrier May 23 '13 at 14:58
  • Sadly the information must be accessible through many forms. :( – user2411504 May 23 '13 at 15:17
  • @orel I find binary's crusade against static variables is weird. Thanks orel, this helps. Seeing as how static variables work to accomplish a goal, I see nothing wrong with using them. If there isn't a problem doing something, then doing it isn't wrong. Silly programmers. I will never understand their syntax arguments. – uFootball8 Dec 08 '13 at 14:10