tags:

views:

513

answers:

9

This is the scenario:

class BaseClass
{
    public virtual string Prop {get; set;}
}

class ChildClass : BaseClass
{
    public override string Prop {get; set;}
}

//program
...
ChildClass instance = new ChildClass;
Console.WriteLine(instance.Prop); //accessing ChildClass.Prop
...

The question is how to access BaseClass.Prop in a instance of ChildClass? Will casting do the trick?

Console.WriteLine((instance as BaseClass).Prop); //accessing BaseClass.Prop

-EDIT- A lot of people suggested casting. In C++ it would not work because polymorphism would still ensure that the child property is called. Isn't that the case in C#?

In C++ you would solve the issue by doing:

instance.(BaseClass::get_Prop())
A: 

You can't do that in a simple way from the outside. Inside ChildClass you can access it through base.Prop.

Note also that casting it will not make any difference. Consider the following code:

class BaseClass
{
    string _prop = "base prop";
    public virtual string Prop
    {
        get { return _prop; }
        set { _prop = value; }
    }
}

class ChildClass : BaseClass
{
    private string _childProp = "child prop";
    public override string Prop
    {
        get { return _childProp; }
        set { _childProp = value; }
    }

    public string PropFromBase
    {
        get { return base.Prop; }
        set { base.Prop = value; }
    }
}


Console.WriteLine((new ChildClass()).Prop);  // writes "child prop"
Console.WriteLine((new ChildClass() as BaseClass).Prop);  // writes "child prop"
Console.WriteLine((new ChildClass()).PropFromBase);  // writes "base prop"

Note however that in your code example you have not overridden the property; you have hidden it, which means that you essentially created a brand new property that just happens to have the same name. In the code example that I show above it is overridden instead, using the virtual keyword in the base class and the override keyword in the child.

Fredrik Mörk
+3  A: 

Cast the instance of ChildClass to an instance of BaseClass; i.e. use:

((BaseClass)childClassInstance).Prop
McWafflestix
Isn't polymorphism still supposed to call the child property in tha case?I'm basing myself in C++ here, maybe C# is different. If so, can someone point that out?
David Reis
@David: the cast works because of what is likely to be a mistake in the original code; the property is in fact not overridden.
Fredrik Mörk
In C#, you have control over polymorphism in this instance; if you give the property/method the "new" keyword, then casting to the base class calls the base class implementation. But if you give the property/method the "override" keyword, the derived class implementation will always be called no matter what.
mquander
This is a library Class, it does not have the new keyword in there and I can't add it. So what you are saying is that my problem has no solution?
David Reis
+1  A: 

Don't you get a compiler error for that code?

In any case, ChildClass.Prop does not override BaseClass.Prop - it hides it. You should really declare it as

    public new string Prop {get; set;}

You can access any member from the base class via base.Member. In this case, base.Prop.

John Saunders
No compiler error -- "new" is assumed. He might get a compiler warning, though.
mquander
Ahah. I guess I've been thinking of a message from ReSharper.
John Saunders
This is new in C# 3.0
Maxime
I mean override to be there. I just added it.
David Reis
@Beaud: what is new in 3.0? The "new" keyword has been there since Day 1.
John Saunders
@David: I'm _certain_ your change will produce a compiler error, since base.Prop is not virtual.
John Saunders
@John: I though he was talking about Automatic Properties: get; set;. My bad :|
Maxime
I mixed up things I think, you're the one that mentioned the compiler error and I though I was answering a comment. haha, ah well!
Maxime
A: 

Just like for any base member: base.Prop

eulerfx
The code accessing the property is not inside the class, so base is no good!
David Reis
+1  A: 
Console.WriteLine(((BaseClass)instance).Prop); //BaseClass.Prop

This works because the property isn't overridden, it's hidden. If you added the "override" keyword to the derived class property, it would no longer work.

mquander
I meant ovveride to be there. I just added it.
David Reis
Then you can't do it. That's the point of override.
mquander
So what you mean is that it is impossible to access teh base property?
David Reis
It's impossible if you're overriding it with another property, yes. If you use the "new" keyword instead, then you can do what you want.
mquander
+1  A: 
BaseClass baseInstance = instance as BaseClass;
Console.WriteLine(baseInstance .Prop); //accessing BaseClass.Prop
James Curran
A: 

Your ChildClass's Prop property is hiding BaseClass's Prop property. This should be a compiler warning.

EDIT: You'll have to decide which behavior you want, polymorphism or new property declaration, since c# allows for both.

  • use virtual/override for polymorphic behavior
  • use new for "new-but-same-name" property behavior
micahtan
got that and fixed it.
David Reis
A: 

Use the new keyword along with the conversion and it should work

such as public new string Prop { [...] } and Console.WriteLine((instance as BaseClass).Prop);

Maxime
+2  A: 

The semantics of overriding a property won't let you access the base of an overridden property. And this makes sense. Classes that use ChildClass shouldn't care if the property is overridden or not. When the property is used by another class it's up to ChildClass to return the appropriate value.

Usually questions like these indicate an attempt to solve a different problem. What kind of problems are you facing that you need direct access to the base property?

Paul Alexander