Can a base class access a derived class?
-
class Parent
{
//how do I access value from here?
}class Child: Parent
{
string value="asdf";
}/\ |_ E X E GG
Define a protected property or method overridable by the derived class. Example class Parent { protected virtual string Value { get{;} } private string GetDerivedValue() { return Value; } } class Child: Parent { string value="asdf"; protected override Value { get{return value;} } }
Visit my blog at http://dotnetforeveryone.blogspot.com/
-
class Parent
{
//how do I access value from here?
}class Child: Parent
{
string value="asdf";
}/\ |_ E X E GG
To access the value there, you need to use the 'as' keyword, as in Parent p = this as Parent; If p != null, then the object was indeed of the parent type, and you can then use the p object to access public properties ( and, I assume, protected ones )
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
To access the value there, you need to use the 'as' keyword, as in Parent p = this as Parent; If p != null, then the object was indeed of the parent type, and you can then use the p object to access public properties ( and, I assume, protected ones )
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
But wouldn't that violate OOP principles? (The parent knowing too much about the child.) Wouldn't the "proper" way be for the parent to specify that the child must have a particular method?
class Parent
{
protected abstract string Value{get;} ;public override string ToString() { return ( Value ) ; }
}
class Child : Parent
{
string value="asdf";protected override string Value { get { return ( this.value ) ; } }
}
(I hope that's correct.)
-
But wouldn't that violate OOP principles? (The parent knowing too much about the child.) Wouldn't the "proper" way be for the parent to specify that the child must have a particular method?
class Parent
{
protected abstract string Value{get;} ;public override string ToString() { return ( Value ) ; }
}
class Child : Parent
{
string value="asdf";protected override string Value { get { return ( this.value ) ; } }
}
(I hope that's correct.)
yes, you're right. The 'proper' was is definately for the property to be defined in the base method. But, if that's not possible, and if there's no way around it, what I described is the way to achieve what was asked for. Another way would be to define the property through an external interface, which is implimented by the derived class, and which is used in the 'as' call in the base class.
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
yes, you're right. The 'proper' was is definately for the property to be defined in the base method. But, if that's not possible, and if there's no way around it, what I described is the way to achieve what was asked for. Another way would be to define the property through an external interface, which is implimented by the derived class, and which is used in the 'as' call in the base class.
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
I don't even think that would work, Parent doesn't declare value. And the further problem would be if a child declares value as some unexpected type or not at all. The base class must either include the declaration of value or specify an abstract method either directly or via an interface.