2

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.
Hazel へいぜる
  • 2,751
  • 1
  • 12
  • 44
  • Is the extension in other dll? – Óscar Andreu Oct 31 '18 at 13:59
  • 7
    I'm not sure what you're asking, but you're not calling `PointToNDC` as an extension method. To do that, you need an instance of `SpriteBatch`. – Brian Rasmussen Oct 31 '18 at 13:59
  • 1
    Are you trying to call the extension on a static class? Does not look like you are trying to call it on an instance of `SpriteBatch` – Nkosi Oct 31 '18 at 13:59
  • 1
    Are you expecting ` SpriteBatch.PointToNDC(new Size2(50, 50), new Point(0, 0));` to call `public static Vector3 PointToNDC(this SpriteBatch sb, Size2 screenSize, Point p) { /*code*/}`? It won't. You need to call it on an instance of a SpriteBatch: `var sb = new SpriteBatch(); var v3 = sb.PointToNDC(/*params*/);` – Flydog57 Oct 31 '18 at 14:02
  • @BrianRasmussen AHHHHHHHHHHHH How did I not see that.... If you write up a brief answer I'll mark it as accepted. I knew it was something simple that I was overlooking. – Hazel へいぜる Oct 31 '18 at 14:03
  • 1
    Since your `PointToNDC` doesn't even do anything with the `sb` parameter, there's probably no point in having it be an extension method at all versus just being a regular static method on a static class. – Matt Burland Oct 31 '18 at 14:04
  • @MattBurland I'm following Frank Luna's DirectX11 book [see this sample](http://www.d3dcoder.net/Data/Resources/SpritesAndText.pdf) and it's all written in C++; the C# library SharpDX doesn't contain a definition for this method though apparently in C++ it has a definition but no implementation. So Frank implements the method; now I could just create a static method but I would like future developers that take this project over to be able to follow the same book if needed. – Hazel へいぜる Oct 31 '18 at 14:06
  • 1
    @PerpetualJ: There's still no sense making it an extension method. You are now in your update creating a throw away `SpriteBatch` just so you can call an extension method which does nothing to the instance of `SpriteBatch` at all. Make the signature of the method `public static Vector3 PointToNDC(Size2 screenSize, Point p)` and call it as `Utilities.PointToNDC(...)` – Matt Burland Oct 31 '18 at 14:08

1 Answers1

3

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

Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317