Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
When I have Inheritance from a Class such as Button, how do the properties of Button work?
While private fields Button Not Inherits to My Class, but properties need to private field to return value(Such as Location Property)?

public class MyClass:Button
{
 	MyClass()
	{
	this.Location = new System.Drawing.Point(134, 34);
	}
}
----------------------------------------------------------------------
///a property of Button Class(.NET Source Code )
public Point Location
{
    get
    {
        return new Point(this.x, this.y);        //x is a private field
    }
    set
    {
        this.SetBounds(value.X, value.Y, this.width, this.height, BoundsSpecified.Location);
    }
}
Posted
Updated 22-Mar-11 4:59am
v6
Comments
Dalek Dave 22-Mar-11 11:00am    
Edited for Grammar.

Deciphering your question.....


DONE.

Did you mean you can't inherit private fields of your base class? If so, you can't. Private are'nt meant to be inherited by other classes. If you want your field to be inherited, change them to protected or add a public property that gets the value of your private field. Read more about access modifiers.
 
Share this answer
 
Comments
Dalek Dave 22-Mar-11 11:00am    
Well done, it was a little ambiguous.
Sergey Alexandrovich Kryukov 22-Mar-11 12:14pm    
Encryption successful, my 5.
(Still I think my Answer is better, will you see? Pretty interesting :-)
--SA
Read this page:

Access Modifiers[^]
 
Share this answer
 
Comments
Dalek Dave 22-Mar-11 11:01am    
Good Link.
Sergey Alexandrovich Kryukov 22-Mar-11 12:13pm    
Very good, a 5, but I know a better answer :-)
(If you did not see it yet you may want to see, it's interesting :-)
--SA
You can't. That's what private means. If you're doing a property override you should use base.PropertyName and use that value for further processing (in this example, do Point loc = base.Location; return new Point(loc.X, loc.Y);).

Also, I'm sure you know this but the example you posted is entirely pointless as that's pretty much what Control.Location does already.
 
Share this answer
 
Comments
[no name] 22-Mar-11 10:10am    
i reflect button class with reflector ,There was exactly this code
Dalek Dave 22-Mar-11 11:00am    
Seems reasonable.
Please find the answer here:

Microsoft Q209354

Access you really need is "internal protected"; your will need just "protected" if your derived class needs to be in other directory.

—SA
 
Share this answer
 
v2

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