Reputation: 3893
I'm trying to create a static(global) function that I can call from within any script in my project using an extension method, but I don't think I am implementing it properly.
file:extensions.cs
namespace CustomExtensions
{
public static class MathExt
{
public static float Remap (float value, float from1, float to1, float from2, float to2)
{
return (((value - from1) * (to2 - from2) / (to1 - from1)) + from2);
}
}
}
Now from within another file I am want to be able to use this syntax:
using CustomExtensions;
public class MySound
{
public void SetPitch(float rpm)
{
pitch = Remap(rpm, minRPM, maxRPM, 0.5f, 1.5f);
}
}
However I get an error unless I do MathExt.Remap(rpm, 720, maxRPM, .75f, 1.75f);
I also tried using CustomExtensions.MathExt;
but it still threw an error out.
I would like to call this function without having to declare MathExt before it. I realize that it's simple enough to just add the classname, but I want to understand what I am doing incorrectly.
Upvotes: 3
Views: 1069
Reputation: 13626
if you are using C#6 you can try and use
using static CustomExtensions.MathExt;
Upvotes: 4
Reputation: 157098
That is not an extension method. You didn't define the object that is the base for the extension method (you do that using this
):
public static float Remap (this float value, float from1, float to1, float from2, float to2)
{ }
Then you call it:
pitch = rpm.Remap(minRPM, maxRPM, 0.5f, 1.5f);
Upvotes: 3