Гена Букин Гена Букин - 29 days ago 6
C# Question

static method invoked via derived type

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?

Answer

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.

Comments