Reputation: 1607
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.[inherited]somemethod() to execute person.somemethod() but with variables coming from man
thank you
Upvotes: 5
Views: 7406
Reputation: 727137
Add a non-static property covering the static string, and reference that property instead:
class person {
private const string request = "select * from person where gender in ('male','female')";
protected virtual string Request {get {return request;}}
public string sharedmethod() {
return "the request is" + Request;
}
}
class man:person {
private const string request = "select person.*,man.* from person,men where mytype in ('male') ";
protected override string Request {get {return request;}}
DateTime dateOfFirstCar;
}
class woman:person {
private const string request = "select person.*,woman.* from person,women where mytype in ('female') ";
protected override string Request {get {return request;}}
DateTime dateOfFirstITBAG;
}
Upvotes: 3