Jump to content
  • Advertisement
Sign in to follow this  
way2lazy2care

Pass by Reference Clarification - C#

This topic is 2858 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok. IIRC C# passes by reference things that are not primitive data types. (Or passes references by value).

Short version:
Whenever I send a Vector2 into a constructor and assign a member to that Vector2, it doesn't reference the same Vector2.

Longer but still pretty short version:
I'm trying to use RoundLine for simple graphics (not my code), and it uses a RoundLine object which has endpoints (Vector2s) that define the line, and a derived class Disc, that has both endpoints set to the same Vector2 to make a dot/disc.

I am trying to make a bunch of Dots that move around the screen, and I made another class that has a Disc member, a Vector2 position, and a velocity with update and whatever methods. I am trying to get the position vector2 in this class to be the same vector referenced by the endpoints in the other class. It appears that they do not reference the same vector2, because the changes in one don't reflect in the other.


In retrospect, I could probably just use the roundline code and make my own class without all the redundant references, but I am just wondering why it is not working as is because it's working contrary to the way I figured it worked.

Share this post


Link to post
Share on other sites
Advertisement
Vector2 is a struct. Structs are passed by value in C#. You need to use the ref keyword.

Share this post


Link to post
Share on other sites
Quote:
Original post by 1863
Vector2 is a struct. Structs are passed by value in C#. You need to use the ref keyword.


D: I assumed structs were treated the same as classes. You know what they say when you assume.... -_-

Thanks though!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!