Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

use extension function to change variable value

Is there a way to use an extension method to change a variable to the return value?

I have a basic remap function

public static float Remap(this float value, float min1, float max1, float min2, float max2)
        => (value - min1) / (max1 - min1) * (max2 - min2) + min2;

distance.Remap(0, 2, 120, 80);

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I want distance to have the value of the return without explicitly setting it like

distance = distance.Remap(0, 2, 120, 80);

>Solution :

ref Should do this just fine.

By making the this float value a ref parameter,
You can alter the input by setting it as your return value.

 public static float Remap(this ref float value, float min1, float max1, float min2, float max2)
    => value = (value - min1) / (max1 - min1) * (max2 - min2) + min2;
    
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading