5

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

0

1 Answer 1

3

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;
}
1
  • 1
    @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. Mar 22, 2012 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.