Click here to Skip to main content
15,904,828 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi there.
i want to create a base class that other class inherits from it.
and when i create an instance of heir class and setting its property
my base class check properties's value are filled or not.
see following:
C#
abstract class Person
{
    //i want to check value of all heir class's property .
}

class Teacher : Person
{
    public string Family { get; set; }
    public string Cellphone { get; set; }
}

class Student : Person
{
    public int Age { get; set; }
    public int Height { get; set; }
}


my inherited class usage(this is not my real application,following code is for understanding):
C#
private void ExamButton_Click(object sender, RoutedEventArgs e)
       {
           Teacher t = new Teacher();

           //i want after instantiate heir class, check their property value.
           //for example
           t.Family = string.Empty;

           //following statement show nothing. but i want all string replaced with "--------"
           //MessageBox.Show(t.Family);
       }

thanks alot.
Posted
Comments
Sergey Alexandrovich Kryukov 31-Dec-14 14:24pm    
It is possible but would be a huge abuse of technology, error-prone, non-maintainable, and so on. Instead, you should design your code in reasonable way.
—SA

1 solution

You can't - and your example shows why: your two derived classes do not have the same properties, and do not share a "family" property.
So what would happen in the base class if you created an instance of a derived class which doesn't contain the property? it can't access it, so you application falls over.

You can do it, but only if you configure your abstract base class to have an abstract property which all derived classes must implement:

C#
abstract class Person
{
    public abstract string Family { get; set; }
}
You can then access the Family property in all derived classes from your based class code.


"Is it possible define a base class then inherit it and check derived class's property value through base class method? please guide me"


If the property is declared as abstract in the base class so that all derived classes have to implement it in order to compile, then the base class can access the derived class value in base class code:
C#
abstract class Person
    {
    public abstract string Family { get; set; }
    public void ShowMe()
        {
        Console.WriteLine(Family);
        } 
    }
That's fine, because the abstract keyword ensures that the Family property must be implemented in all derived classes. However, the base class can't get at anything the derived class uses to generate the values in the property:
C#
abstract class Person
    {
    public abstract string Family { get; set; }
    public void ShowMe()
        {
        Console.WriteLine(Family);
        }
    }
class Individual : Person
    {
    public string HyphenatedStart { get; set; }
    public string HyphenatedEnd { get; set; }
    public override string Family
        {
        get
            {
            return string.Format("{0}-{1}", HyphenatedStart, HyphenatedEnd);
            }
        set
            {
            string[] parts = value.Split('-');
            HyphenatedStart = parts[0];
            HyphenatedEnd = parts[1];
            }
        }
    }
The Person.ShowMe method can't access HyphenatedStart or HyphenatedEnd because they don't exist as part of the Person class, and so aren't guaranteed to exist in all derived classes - even though they are public values in the Individual class.
 
Share this answer
 
v2
Comments
aliwpf 31-Dec-14 10:26am    
Is it possible define a base class then inherit it and check derived class's property value through base class method? please guide me
OriginalGriff 31-Dec-14 10:50am    
Answer updated

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900