This is new for me so I'm not really sure how to do this...

I'm trying to create a base class that I can inherit from but there are some fields that I need to ensure each inheriting class specifies a value for. How do I do that?

Recommended Answers

All 5 Replies

Look into abstract properties. Example:

class Program
    {
        static void Main(string[] args)
        {
            MyDerived myDerived = new MyDerived();
            myDerived.ID = "A";
            myDerived.TestProperty = "OK";
        }
    }

    public abstract class MyBase
    {
        public string ID { get; set; }
        public abstract string TestProperty { get; set; }
    }

    public class MyDerived : MyBase
    {
        public override string TestProperty { get; set; }
    }

Thanks for the reply

Do abstract properties ensure a value, or just that the inheriting class explicitly defines the get and set? I may be wrong in thinking there's a way to do this or that inheritance is even the right way to go...

Abstract properties are much like an interface; when you inherit from the base class, you are in essence signing a contract saying that you will implement the property from the base. However, that doesn't necessarily mean that the property has to be used in any meaningful manner.

I assume there is a method or something in your base or perhaps somewhere else that performs an action but it is contingent on the property being populated with something other than the default value for a type (such as null for a string or 0 for an int)? If so, nothing beats simply testing the property before execution.

public abstract class MyBase
    {
        public string ID { get; set; }
        public abstract string TestProperty { get; set; }

        public void DoSomething()
        {
            if (!string.IsNullOrEmpty(this.TestProperty ))
            {
                // do something
            }
        }
    }

I should say the abstract properties and methods (much like an interface) are useful when you have a common behavior you might want derived classes to share but you will leave it up to the derived classes to actually define the implementation. MyFile and MyDatabase could inherit from the same base class and the base might define an abstract Save() method. It would then be up to MyFile and MyDatabase to define specific implementations of that method, as obviously what works for MyFile wouldn't fit the needs of MyDatabase.

Upon re-reading your initial problem, perhaps abstraction isn't particularly relevant for you right now, perhaps it is.

>>scranton:

Why don't you explain what you're trying to accomplish in more detail? apegram pointed you in a good direction with abstraction but sometimes you run across odd scenarios where you don't necessarily want to implement it (namely an abstract base form you inherit from, it is possible to do properly but a pain).

i have some web controls that do similar things. i'm trying to eliminate duplicate code. inheritance may not have been the right approach. i probably just need a reusable web control... apegram at least answered my question, but now i need to rethink the problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.