user1306322
user1306322

Reputation: 8721

How to resolve error with equally named types in method parameters?

I created two overload methods with similarly named (but not the same) types of parameters, and their declaration looks like this:

public static RectangleNineSides GetRectangleSide(System.Drawing.Rectangle rect, System.Drawing.Point p)
public static RectangleNineSides GetRectangleSide(Microsoft.Xna.Framework.Rectangle rect, Microsoft.Xna.Framework.Point p)

As you can see, the parameters are different in both methods, but when calling one of them like so:

h.RectangleNineSides side = h.GetRectangleSide(new Microsoft.Xna.Framework.Rectangle(), new Microsoft.Xna.Framework.Point());

In my XNA project I get an error saying:

The type 'System.Drawing.Point' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Drawing, Version=4.0.0.0, blah-blah'

But why does the compiler care about the other method if it isn't even called anywhere in the current project?

Analogous error appears in any WinForms project but for XNA's types. I even explicitly wrote which Rectangle and Point I'm talking about, and I still get this error. What am I doing wrong? Is it not possible to have overloaded methods without getting this kind of error? Should I just change their names?

Upvotes: 1

Views: 97

Answers (1)

Karl Anderson
Karl Anderson

Reputation: 34844

The compiler cares, because it compiles code that may or may not be called; nevertheless it still needs to compile the code and be able to resolve where the type is defined.

I would recommend not putting the differing technology drawing methods in your helper class, but if you, then I would definitely name them differently, like this:

public static RectangleNineSides GetRectangleSide(System.Drawing.Rectangle rect, 
                                                  System.Drawing.Point p)
public static RectangleNineSides GetXnaRectangleSide(Microsoft.Xna.Framework.Rectangle rect, 
                                                  Microsoft.Xna.Framework.Point p)

Upvotes: 1

Related Questions