Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 642
Гена Букин Avatar asked May 18 '15 08:05

Гена Букин


People also ask

How do you call a static method in base class?

A static method is the one which you can call without instantiating the class. If you want to call a static method of the superclass, you can call it directly using the class name.

Why do we use static methods in C#?

Static methods are usually useful for operations that don't require any data from an instance of the class (from this ) and can perform their intended purpose solely using their arguments.

When to use static classes C#?

Static classes are used as containers for static members. Static methods and static properties are the most-used members of a static class. All static members are called directly using the class name. Static methods do a specific job and are called directly using a type name, rather than the instance of a type.


2 Answers

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.

like image 117
Yuval Itzchakov Avatar answered Oct 12 '22 12:10

Yuval Itzchakov


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.

like image 31
MakePeaceGreatAgain Avatar answered Oct 12 '22 13:10

MakePeaceGreatAgain