Click here to Skip to main content
14,243,763 members
Rate this:
Please Sign up or sign in to 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.
Rate this:
Please Sign up or sign in to vote.

Solution 2

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.
   
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
Rate this:
Please Sign up or sign in to vote.

Solution 3

Read this page:

Access Modifiers[^]
   
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
Rate this:
Please Sign up or sign in to vote.

Solution 1

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.
   
Comments
Mehdi Hadeli 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.
Rate this:
Please Sign up or sign in to vote.

Solution 4

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
   
v2

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100