static method invoked via derived type
Asked Answered
G

2

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?

Glomerule answered 18/5, 2015 at 8:9 Comment(1)
At a guess, Canvas is a subtype of another type and SetZIndex is defined by the latter; not by Canvas.Ab
U
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.

Upshaw answered 18/5, 2015 at 8:26 Comment(9)
Downvoted for suggesting warnings aren't a problem. Good advice would be to treat warnings as errors and to fix them.Ab
@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.Upshaw
@YuvalItzchakov No this warning isn't safe. It could result in serious problem. Refer my answer for details.Lobster
@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.Upshaw
If there is a case like that, you can't say "In this particular case, the warning may be safely ignored"Lobster
@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.Upshaw
@SriramSakthivel I've edited my answer so i'm clear as to what I mean.Upshaw
@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.Lobster
@SriramSakthivel Yes, I see what you mean.Upshaw
C
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.

Carniola answered 18/5, 2015 at 8:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.