9

There is a class, called circle. It contains circleID, circleGeometry and circlePath propertities.

public class Circle 
{
    public string ID {get; set;}
    public EllipseGeometry circleGeometry {get; set;}
    public Path circlePath {get; set;}
}

Now, I'm trying to set ZIndex value for a circle. For example it'll be 2.

Canvas.SetZIndex(someCircleID.circlePath,2);

But I have such kind of warning:

"static method invoked via derived type"

Can somebody explain me what does that mean?

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
  • At a guess, `Canvas` is a subtype of another type and `SetZIndex` is defined by the latter; not by `Canvas`. – David Arno May 18 '15 at 08:19

2 Answers2

8

SetZIndex is defined on the Panel class, which Canvas derives from. The compiler is generating a warning saying you're using a static method on a sub-type. This isn't an actual problem, but it may lead to confusions when used in certain ways. As SetZIndex is void returning, that shouldn't be a problem.

But imagine the following:

var ftpRequest = (FtpWebRequest) HttpWebRequest.Create("ftp://my.ftp.com");

Create is actually a static method of WebRequest, but is used on HttpWebRequest instead, because it is a derived-type and you may do so. So, you'd expect it to be a web request which is being generated, right? But it isn't, it generates a FtpWebRequest, because that's specified in the URI.

Edit:

I want to point out that generally, the compiler warnings are there for a reason, that is way this one exists as well. As long as there is no overload of SetZIndex created in the Canvas class, the call is safe. But as @SriramSakthivel points out in the comments, if any extension method or static method is added to the Canvas class (using the new modifier) along the way, by you or by anyone else, it will no longer output the desired result and you must be aware of that.

AP Datum
  • 15
  • 4
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
  • Downvoted for suggesting warnings aren't a problem. Good advice would be to treat warnings as errors and to fix them. – David Arno May 18 '15 at 10:11
  • @David Nowhere did I attempt to say that all warnings aren't a *problem*. They're there to indicate something may be wrong. In this particular case, the warning may be safely ignored. – Yuval Itzchakov May 18 '15 at 10:16
  • @YuvalItzchakov No this warning isn't safe. It could result in serious problem. Refer my answer for details. – Sriram Sakthivel May 18 '15 at 10:17
  • @Sriram That is a hypothetical edge case where adding a static method would be a breaking change to the code. If yoy made that static method, it is your responsibility to make sure you didn't break anything. If it's a framework *breaking change*, it's still your responsibility to make sure nothing breaks. – Yuval Itzchakov May 18 '15 at 10:20
  • If there is a case like that, you can't say "In this particular case, the warning may be safely ignored" – Sriram Sakthivel May 18 '15 at 10:57
  • @SriramSakthivel At the *current point in time*, as there is no such overload of the method, it can be safely ignored. If anything changes in current state, then this is no longer safe. That is what i mean. – Yuval Itzchakov May 18 '15 at 11:00
  • @SriramSakthivel I've edited my answer so i'm clear as to what I mean. – Yuval Itzchakov May 18 '15 at 11:02
  • @YuvalItzchakov Small correction. Overload method isn't a problem, but `new` method is. I mean a method declared with same signature in derived class will cause the problem. – Sriram Sakthivel May 18 '15 at 11:04
  • @SriramSakthivel Yes, I see what you mean. – Yuval Itzchakov May 18 '15 at 11:07
2

This means that the method SetZIndex is defined on a base-type of class Canvas but you call it using the latter. You´d better be off by using the base-class.

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111