Question

I am using a list for particles.

List<Particle> particles;

Normally i place this list in my Simulation class. Which calculates position, velocity and other properties of particles.

A few other classes need this particle data for output and post processing.

is it OK to create a static class,

static class Particles
{
    static List<Particles> plist;
}

to access particle data from other classes?

Was it helpful?

Solution

I would recommend staying away from static classes/methods whenever possible. They tend to lead to high coupling of code. Although there are some cases where it's far faster to use them (hopefully sparingly).

I'm not quite sure what you are going for from your question, but I would at least recommend changing the static class to expose a property instead of a field.

public static class Particles
{
    public static List<Particles> PList { get; set; }
}

or

public static class Particles
{
    private static List<Particles> _plist;

    public static List<Particles> PList
    {
        get { return _plist; }
        set { _plist = value; }
    }
}

This way you encapsulate the list a little more. For example, you can check for null values during the getter or setter.

OTHER TIPS

You have at least two options here:

  1. Create an IList<Particles> property in each class that operates on particles.

  2. In each class that operates on particles, create a private IList<Particles> field and a constructor that takes such a list as a parameter.

Either of these options will preserve encapsulation of the list.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top