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();
}
This code is invalid to start with:
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:
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.