C# why can't we override static property? (how can high level classes call base class method with high level data)?

Add a non-static property covering the static string, and reference that property instead.

Up vote 0 down vote favorite share g+ share fb share tw.

My question is probably not well phrased and it's probably a dupe but here I go class person { protected static string request = "select * from person where gender in ('male','female')"; public string sharedmethod() { return "the request is" + request; } } class man:person { override protected static string request = "select person. *,man. * from person,men where mytype in ('male') "; DateTime dateOfFirstCar; } class woman:person { override protected static string request = "select person.

*,woman. * from person,women where mytype in ('female') "; DateTime dateOfFirstITBAG; } class Program { static void Main(string args) { if (new person().sharedmethod() == new man().sharedmethod()) Console. Write("too bad query is the same in base and dervide class"); } } A man is a person A woman is a person Person,man,woman exist in my database but need different queries I don't want to duplicate those queries so I thought it was a good idea to store them in a static property in each class.

I got some low level stuff (not figured there) that lie in the base class (coz I don't want to duplicate) and I wanted inherited classes to call base class method with the context of the herited classes I want man. Inheritedsomemethod() to execute person.somemethod() but with variables coming from man thank you c# inherit link|improve this question edited Mar 22 at 14:09 asked Mar 22 at 12:04frenchone727 50% accept rate.

2 You need to start accepting answers to your questions (go through your old questions, find the answers that most helped you and click the checkmark next to those answers - one per question). – Oded Mar 22 at 12:08 Here is a very useful link that will increase your SO reputation, earn you a brand-new badge, and might even help you get answers to your future questions faster :) – dasblinkenlight Mar 22 at 12:27 I'll accept helpfull (non-self) answers. Right now I don't have many..........Ok I missed some of them 2 because I can't login some time ago.

I check those ones. – frenchone Mar 22 at 13:38.

Add a non-static property covering the static string, and reference that property instead: class person { protected abstract string Request {get;} public string sharedmethod() { return "the request is" + Request; } } class man:person { private static string request = "select person. *,man. * from person,men where mytype in ('male') "; protected string Request {get {return request;}} DateTime dateOfFirstCar; } class woman:person { private static string request = "select person.

*,woman. * from person,women where mytype in ('female') "; protected string Request {get {return request;}} DateTime dateOfFirstITBAG; }.

– frenchone Mar 22 at 13:20 @frenchone I missed that part. You need to make the property virtual instead of abstract, add an implementation in the person class, and add an override to the derived implementations. – dasblinkenlight Mar 22 at 13:22 Ok I'll try that – frenchone Mar 22 at 13:39.

Because the .net designers decided that it was more trouble than it was worth to add that facility to the framework. As for dealing with your design, there are lots of ways to make it better... One would be to have a static helper class, put the sql in that as static strings and add a static method to return it, then call it from Person, Man Woman, etc. put an enum in Person and a static GetSql in the helper class and it's job done. Oh and sort out your accept rate, otherwise people aren't going to help you.

Oh and sort out your accept rate, otherwise people aren't going to help you. My post get off-topic response or no response at all. – frenchone Mar 22 at 13:31 > As for dealing with your design, there are lots of ways to make it better... So my question is "how to make it better"?

> a static helper class I thought hellper class was evil? – frenchone Mar 22 at 13:34 No class is evil, you can use them for evil though. Is a static factory pattern evil?

. Net is chock full of static helper classes. So is the Delphi VCL and you can overide static methods in that.

If it's clear readable and fit for purpose then it's good. Some things there's just no other sensible way to do in an OO framework. – Tony Hopkinson Mar 23 at 12:40.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions