Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6184923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:37:22+00:00 2026-05-24T01:37:22+00:00

I am trying to access an object which is a member of an object

  • 0

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();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T01:37:23+00:00Added an answer on May 24, 2026 at 1:37 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have the following method where we are trying to access an array object
Need some quick advice I am trying to access a object array but I
I am trying to access member variables of a class without using object. please
Possible Duplicate: javascript object, access variable property name? I'm trying to call a method
I am using .Net Remoting and trying to access a remote object modified in
I am trying to access information on a website that uses frames. When I
I have an array of 1000-2000 elements which are pointers to objects. I want
I'm trying my hand at creating a session which stores member information which the
i created a singleton class and trying to access that class in other class
In C++, I have an Object class, from which a number of other classes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.