Jump to content
  • Advertisement
Sign in to follow this  
Tom

[.net] [C#] Extension methods on value types?

This topic is 3321 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

Here's some code I drew up:
public static void Translate(this Matrix3x2 m, float x, float y)
{
    m.M31 += x;
    m.M32 += y;
}

I have several methods that look like this, for scaling and rotation. Naturally it doesn't work because value types are copied rather than passed by reference, so the original matrix elements aren't actually being set. My question: is there a way around this? (Bear in mind 'ref' can't be used with 'this'.) Thanks in advance for any help.

Share this post


Link to post
Share on other sites
Advertisement
You'll have to return your value type, and call like so:


public static Matrix3x2 Translate(this Matrix3x2 m, float x, float y)
{
m.M31 += x;
m.M32 += y;

return m;
}
.
.
.

matrix = matrix.Translate(4.44f, 2.11f);


Share this post


Link to post
Share on other sites
Right, I figured it wasn't possibly to operate directly on the matrix. I have functional variants that return values, so those will have to suffice. Thanks.

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!