Let's say I'm writing a space game (just as an example, I'm not).

Let's say that in this game I want to have the ability to create 300 different kinds of ships, 500 different crew members and a 100 different planets.

When I start out, there's 0 of any of these ... I haven't built the ships, I haven't hired the crew, and I haven't discovered any planets.

Now ... just about every single aspect of my program (including just about every form) is going to operate on these ships, crewmen, and planets to one degree or another, or at minimum display data for them.

And every single one of them is built through inheritance.

How does any of that work in a static class? From what I'm reading it really doesn't.

So with a static class not available (doesn't deal with inheritance) and no real option for global scope ... how am I going to make this these objects available throughout my application?

I don't want to be writing this stuff out to a file (or DB) and pulling them back in, in every method I need to access them (which might be every one). That would be silly and slow.

I can't believe this isn't possible in C# ... but everything you read on the web is either extremely convoluted, or people just cop a defensive attitude and start putting down the people asking the question (not me, this is the first time I've asked anywhere).

I don't need specific code, this is a discussion. Please give me some thoughts and point me in the direction of what I need to be learning about.

Recommended Answers

All 3 Replies

Globals are a bad OO design, C# is an OO language. The module in vb.net is just a neat trick the compiler offers. The modules are basically just sealed classes with all their members declared as static. You can do the same yourself, you just don't promote the member names into the global address space so you have to prefix them with the class name.
Here's a basic outline of what i'm talking about. You have a Cruiser class inheriting from a Ship class. You have your sealed DataStore calss (a'la module) with static members. You can then store your inherited class objects in the datastore and access them anywhere in your application:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DataStore.cruisers.Add(new Cruiser());
            MessageBox.Show(DataStore.cruisers[0].CrewSize.ToString());
        }
    }

    class Ship
    {
        public void Fly()
        {
        }
    }

    class Cruiser : Ship
    {
        public int CrewSize = 30;
    }

    sealed class DataStore
    {
        public static List<Cruiser> cruisers = new List<Cruiser>();
    }

Globals are a bad OO design, C# is an OO language. The module in vb.net is just a neat trick the compiler offers. The modules are basically just sealed classes with all their members declared as static. You can do the same yourself, you just don't promote the member names into the global address space so you have to prefix them with the class name.
Here's a basic outline of what i'm talking about. You have a Cruiser class inheriting from a Ship class. You have your sealed DataStore calss (a'la module) with static members. You can then store your inherited class objects in the datastore and access them anywhere in your application:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DataStore.cruisers.Add(new Cruiser());
            MessageBox.Show(DataStore.cruisers[0].CrewSize.ToString());
        }
    }

    class Ship
    {
        public void Fly()
        {
        }
    }

    class Cruiser : Ship
    {
        public int CrewSize = 30;
    }

    sealed class DataStore
    {
        public static List<Cruiser> cruisers = new List<Cruiser>();
    }

OK, thanks Ryshad ... I clearly need to learn more about LISTS. This is the second or third time I've been pointed to a LIST solution.

I'm going to mark this as solved until I understand lists and get myself into more trouble.

Thanks again!

Lists are VERY handy to understand. They are essentially a dynamic array; you don't have to declare the size when you instantiate it, you just create it then add and delete items as necessary

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.