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 78471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:59:33+00:00 2026-05-10T20:59:33+00:00

I don’t have too much experience with C# so if someone could point me

  • 0

I don’t have too much experience with C# so if someone could point me in the right direction I would greatly appreciate it. I have a foreach loop that references a variable of an object. I wish to make another foreach loop inside the main one that compares (or performs actions on) the current variable to the rest of the variables in the array of the object. I have the following code:

// Integrate forces for each body.     foreach (RigidBodyBase body in doc.Bodies)     {         // Don't move background-anchored bodies.         if (body.anchored) continue;          // This is where we will add Each Body's gravitational force          //  to the total force exerted on the object.          // For each other body, get it's point and it's mass.              // Find the gravitational force exterted between target body and looped body.                 // Find distance between bodies.                     // vector addition                 // Force = G*mass1*mass2/distance^2             // Find vector of that force.             // Add Force to TotalGravityForce         // loop until there are no more bodies.         // Add TotalGravityForce to body.totalForce      } 
  • 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. 2026-05-10T20:59:34+00:00Added an answer on May 10, 2026 at 8:59 pm

    Each time you execute foreach, (even while nesting them) the internal Enumerator should ‘new’ up a new iterator for you, there should not be any issue with that. The issues come about when you are adding, or removing items from the collection while you are still iterating…

    Remember, in the inner foreach, to check to make sure you are not on the same item as the outer for each is on

      foreach( RigidBodyBase body in doc.Bodies)      foreach ( RigidBodyBase otherBody in doc.Bodies)          if (!otherBody.Anchored && otherBody != body)  // or otherBody.Id != body.Id -- whatever is required...                // then do the work here 

    by the way, the best place to put this code would be in a GravityForce property of the RigidBodyBase class, Then you could just write:

       foreach (RigidBodyBase body in doc.Bodies)        body.TotalForce += body.GravityForce;  

    although depending on what you’re doing here (moving all objects?) they’re may be even more opportunity for refactoring… I’d also consider having a separate property for the ‘other’ forces, and have TotalForce Property do the summing of the Gravity Force and the ‘other’ Forces?

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

Sidebar

Ask A Question

Stats

  • Questions 72k
  • Answers 72k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Enumerable<Course> FindCoursesForStudent(Student student) { return from enrolment in Enrolments where… May 11, 2026 at 1:41 pm
  • added an answer It has to do with the fact that SQLite has… May 11, 2026 at 1:41 pm
  • added an answer I think you are missing the jquery lightbox stylesheet file.… May 11, 2026 at 1:41 pm

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.