C# value type and reference type problem

Hi guys,

I am having a rather confusing moment here, but the problem is more complex then the title… sorry I could not think of a better one for this topic.

As a side note, I am developing some custom classes inside visual studio using c#, then I reference the .dll file in to a c# component for testing functionality .

The problem I am having right now is with my Vec3 struct and my SharpParticle class. The SharpParticle’s constructor takes a Vec3 position, a Vec3 velocity… and so on.

The problem I am having is that When Vec3 is a struct and is used for the constructor of the SharpParticle object, the particle wont update its position successfully. But If I change Vec3 to be a class the particle will change its position successfully. The strange thing that I don’t understand is that I have done this operation many times in the past using RhinoCommon types. A classic example can be a flocking behaviour were an object of type Agent also takes a Point3d position, Vector3d velocity… etc… and the agents positions are updated successfully in the RhinoCommon world.

Any clues would be very helpful. Let me know If the source code of Vec3 and SharpParticle is needed.

You can see my code here:

using SharpMatter.SharpGeometry;
using SharpMatter.SharpPhysics;
using SharpMatter.SharpForces;

public class Script_Instance : GH_ScriptInstance

  private void RunScript(bool run, bool reset, ref object A, ref object B)

    //  particle.Update();
      a += new Vec3(0.01, 0.04, 0);

    //  particle.Acceleration = Vec3.Zero;
    //  particle.Position = Vec3.Zero;
     vecTest  = Vec3.Zero;


    //A = (Point3d) particle.Position; //Particle's position does not update when Vec3 is a struct, but it does when Vec3 is a class

    A = (Point3d) vecTest ; // Vec3 position updates fine when Vec3 is a struct or a class


  // <Custom additional code> 

  SharpParticle particle = new SharpParticle(Vec3.Zero, Vec3.Zero, new Vec3(0.01, 0.04, 0), 0.03, 0.03, 10, 100);

  Vec3 vecTest = Vec3.Zero;

  // </Custom additional code> 

You‘ll need to post the rest as well. Something which immediately catches my eye is that you are using the same naming convention for local variables and private fields. Usually if you don‘t differentiate here, then there could be a problem with weird clashes inbetween.

So you may change a local variable called particle, but you expect to change the field of particle. The reason why it could work then for a class is that you passing the particle by reference within the method but you expect it not to do so.So if its passed like this the local and class global variable are identical, whereas a struct always copies, as you already know.You always pass a class by reference even if you you are not using the ref keyword.
Usually private fields are marked with _someField or m_someField.

But you know sometimes weird bugs usually cause other bugs, and leave you clearly blind on the real cause…

Hi Tom,
thank you for your reply. I am aware of the differences between a struct and a class, but I still don’t understand the problem that I am facing. As specified in my previous post, if I am just updating a Vec3 position there wont be any problem. The problem arises when Vec3 is passed on as a SharpParticle's position, then the SharpParticle's position is not successfully updated. Attached are the .cs files of the Vec3 struct and the SharpParticle class

Vec3.cs (22.2 KB)

SharpParticle.cs (6.1 KB)

Anyway… there was a silly mistake from my side… I have my physics engine on another class, so its update function mas making a copy of the of the Vec3. I just had to modify the function to pass the arguments asref. Easy peasy.