1

I am trying to access an object which is a member of an object array, is that possible ?

I have declared a structure named Particle, and initialized an object array of "Particle" about 40 particles,now I need to access each particle, for ex: particle.Gbest any one can help ??

here is my code:

struct particle
{
    double[] position = new double[100];
    double Gbest, Lbest;
    double Pconst = 0.5;
}

object[] swarm = new object[swarm_size];

for (int i = 0; i < swarm_size; i++)
{
    swarm[i] = new particle();
}
2
  • You don't have an array of Particle You have an array of object. Commented Jul 27, 2011 at 16:16
  • Instead of object[] you should be using particle[].. beyond that I'm not sure what you're asking? Commented Jul 27, 2011 at 16:17

5 Answers 5

5

This code is invalid to start with:

struct particle
{
    double[] position = new double[100];
}

You can't specify variable initializers for instance variables in structs.

However, accessing data within another object or value is easy - if it's accessible. In this case your fields are private and you haven't provided any access methods or properties, so you won't be able to get at them "from the outside" without more code.

Here's some modified code:

public class Particle
{
    private readonly double[] positions = new double[100];

    // TODO: Rename these to something useful
    public double Gbest { get; private set; }
    private double Lbest;
    private double Pconst = 0.5;

    public Particle(int g)
    {
        Gbest = g; // Or whatever
    }
}


List<Particle> swarm = new List<Particle>();
for (int i = 0; i < swarmSize; i++)
{
    swarm.Add(new Particle(i));
}

double total = 0;
foreach (Particle particle in swarm)
{
    total += particle.Gbest;
}

Now this isn't doing anything particularly useful, because it's not clear what you're trying to do - but I would suggest you get an introductory book on C#. Stack Overflow is great for specific questions, but I think you're early enough on your journey into C# that a good book or tutorial would help you more.

Sign up to request clarification or add additional context in comments.

Comments

1
var swarm = new particle[swarm_size];

for (int i = 0; i < swarm_size; i++)
{
    swarm[i] = new particle();
}

To access properties of the array you could walk it like this:

for (int i = 0; i < swarm_size; i++)
{
     Console.WriteLine(swarm[i].Gbest);
     Console.WriteLine(swarm[i].Lbest);
}

EDIT: swarm declaration was incorrect.

4 Comments

I don't think that works. The compiler does not no that your object[] contain particle types. Perhaps you made a mistake with declaring swarm as an object array?
@musefan: In my sample the swarm is defined as new particle[swarm_size], which gives the compiler enough information about the data contained within.
well my compiler (using VS 2010 Net 4.0) didn't like it. Only recognised "object" properties
@musefan: I see what you mean, I missed that the first time around. Thanks!
0

If you know the index of the particle, you can simply use:

particle part = (particle)swarm[i];
// do stuff with part

If you don't know the index, you can iterate over them:

foreach( particle part in swam )
{
    // do stuff with part
}

Or you can use Linq-to-Objects to do more sophisticated stuff, but I think you need to start with the simple stuff...

By the way, if you can, declare swarm as:

particle[] swarm = new particle[swarm_size];

and get the benefit of type-safety.

Comments

0
struct particle
    {
        public double[] position = new double[100];
        public double Gbest, Lbest;
        public double Pconst = 0.5;
    }

     particle[] swarm = new particle[swarm_size];
         for (int i = 0; i < swarm_size; i++)
           {
            swarm[i] = new particle();
           }

    foreach(particle p in swarm)
    {
       // do magic here
    }

Comments

0

You can either change your array type to Particle and access it with index like...

particle[] swarm = new particle[swarm_size];
particle firstParticle = swarm[0];

or you can cast the object back to a particle...

particle firstParticle = (particle)swarm[0];

I would recommend the first option thou for performance reasons.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.