Taco
Taco

Reputation: 2923

Extension method not found?

So after some looking around and attempting to correct this issue myself, I'm stuck. I looked at the following posts and ensured that I've included all assemblies as they stated (which I had done prior to coming here, but double checked to make sure):

After double checking my file that has the extension, and the file attempting to use the extension; are there any other possible reasons why an extension method wouldn't be found?

// Extension Class.
using SharpDX;
using SharpDX.Direct2D1;
namespace MyNamespace.Engine {
    public static class Utilities {
        public static Vector3 PointToNDC(this SpriteBatch sb, Size2 screenSize, Point p) {
            float x = 2.0f * p.X / screenSize.Width - 1.0f;
            float y = 1.0f - 2.0f * p.Y / screenSize.Height;
            return new Vector3(x, y, 0);
        }
    }
}

// Usage Class.
using MyNamespace.Engine;
using SharpDX;
using SharpDX.Direct2D1;
namespace MyNamespace.Prefabs {
    public class Sprite {
        public void Draw() {
            SpriteBatch.PointToNDC(new Size2(50, 50), new Point(0, 0));
        }
    }
}

Note

Any typos in the code are actual typos here not in the code itself.


Update

As @Brian Rasmussen pointed out in the comments, I didn't call the method from an instance of the object being extended. I haven't had my coffee yet, so my apologies, at least this was a simple fix!

SpriteBatch sb = new SpriteBatch(...);
sb.PointToNDC(...); // <- Works.

Upvotes: 2

Views: 271

Answers (1)

Brian Rasmussen
Brian Rasmussen

Reputation: 116401

To call PointToNDC as an extension method you need an instance of SpriteBatch.

Upvotes: 3

Related Questions