I'm trying to figure out how to subclass a control and override some of the default properties. I can subclass a control and add my own new properties and set their default values. But how do you change the default value for an *existing* property?
For a custom property I can do this:
[DefaultValue(FormatType.Default)]
public FormatType FormatAs
{
get { return this.formatAs; }
set { this.formatAs = value; }
}
But when I try that code structure with an existing property it won't compile. What am I missing?
TIA,
Mike Rodriguez 11 1515
You'll have to "override" or "new" the inherited property and then you can
provide your own implementation, or call the "base" to allow the base to
provide the implementation, and then provide the proper attributes, as you
have done.
--
Tim Wilson
..Net Compact Framework MVP
"Michael Rodriguez" <mi**@nospamforme.com> wrote in message
news:eQ**************@TK2MSFTNGP14.phx.gbl...
I'm trying to figure out how to subclass a control and override some of the
default properties. I can subclass a control and add my own new properties
and set their default values. But how do you change the default value for
an *existing* property?
For a custom property I can do this:
[DefaultValue(FormatType.Default)]
public FormatType FormatAs
{
get { return this.formatAs; }
set { this.formatAs = value; }
}
But when I try that code structure with an existing property it won't
compile. What am I missing?
TIA,
Mike Rodriguez
There are several methods to changing the default value for an existing
property.
The quickest and dirtiest way is either in your constructor or in the
OnInit field to set the property with a specific value
override OnInit(...)
{
FormatType = MyFormatType.Bleah;
.....
base.OnInit(...);
}
I'll speak from a Windows Forms perspective, since I'm not a web developer.
The problem with this method is that, while you will change the initial
value of the property, it will serialize this value into the
InitializeComponent method and show it in bold in the properties window. If
this is not a problem then, well, it's not a problem. But for a nice
design-time experience, using the DefaultValueAttribute for simple defaults,
and the ShouldSerializeX and ResetX methods for complex defaults, is the way
to go.
--
Tim Wilson
..Net Compact Framework MVP
<ma**********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com... There are several methods to changing the default value for an existing property. The quickest and dirtiest way is either in your constructor or in the OnInit field to set the property with a specific value override OnInit(...) { FormatType = MyFormatType.Bleah; .... base.OnInit(...); }
Tim,
I got the override to work. I discovered that it works on some properties
and not others, depending on how the property was originally declared.
Thanks,
Mike
"Tim Wilson" <TIM(UNDERSCORE)WILSON(AT)ROGERS(PERIOD)COM> wrote in message
news:eI*************@TK2MSFTNGP09.phx.gbl... You'll have to "override" or "new" the inherited property and then you can provide your own implementation, or call the "base" to allow the base to provide the implementation, and then provide the proper attributes, as you have done.
-- Tim Wilson .Net Compact Framework MVP
"Michael Rodriguez" <mi**@nospamforme.com> wrote in message news:eQ**************@TK2MSFTNGP14.phx.gbl... I'm trying to figure out how to subclass a control and override some of the default properties. I can subclass a control and add my own new properties and set their default values. But how do you change the default value for an *existing* property?
For a custom property I can do this:
[DefaultValue(FormatType.Default)] public FormatType FormatAs { get { return this.formatAs; } set { this.formatAs = value; } }
But when I try that code structure with an existing property it won't compile. What am I missing?
TIA,
Mike Rodriguez
Yeah. If the base property is marked "virtual" or "override", then you
should "override". In contrast, if the base property is not marked "virtual"
or "override", then you'd use "new".
--
Tim Wilson
..Net Compact Framework MVP
"Michael Rodriguez" <mi**@nospamforme.com> wrote in message
news:u8*************@TK2MSFTNGP15.phx.gbl... Tim,
I got the override to work. I discovered that it works on some properties and not others, depending on how the property was originally declared.
Thanks,
Mike
"Tim Wilson" <TIM(UNDERSCORE)WILSON(AT)ROGERS(PERIOD)COM> wrote in message news:eI*************@TK2MSFTNGP09.phx.gbl... You'll have to "override" or "new" the inherited property and then you
can provide your own implementation, or call the "base" to allow the base to provide the implementation, and then provide the proper attributes, as
you have done.
-- Tim Wilson .Net Compact Framework MVP
"Michael Rodriguez" <mi**@nospamforme.com> wrote in message news:eQ**************@TK2MSFTNGP14.phx.gbl... I'm trying to figure out how to subclass a control and override some of the default properties. I can subclass a control and add my own new properties and set their default values. But how do you change the default value
for an *existing* property?
For a custom property I can do this:
[DefaultValue(FormatType.Default)] public FormatType FormatAs { get { return this.formatAs; } set { this.formatAs = value; } }
But when I try that code structure with an existing property it won't compile. What am I missing?
TIA,
Mike Rodriguez
Tim,
If the base property is not marked virtual or override, and I use new, what
will that do? Will it still keep the properties of the base from which it
inherits?
Thanks,
Mike
"Tim Wilson" <TIM(UNDERSCORE)WILSON(AT)ROGERS(PERIOD)COM> wrote in message
news:%2******************@tk2msftngp13.phx.gbl... Yeah. If the base property is marked "virtual" or "override", then you should "override". In contrast, if the base property is not marked "virtual" or "override", then you'd use "new".
-- Tim Wilson .Net Compact Framework MVP
"Michael Rodriguez" <mi**@nospamforme.com> wrote in message news:u8*************@TK2MSFTNGP15.phx.gbl... Tim,
I got the override to work. I discovered that it works on some properties and not others, depending on how the property was originally declared.
Thanks,
Mike
"Tim Wilson" <TIM(UNDERSCORE)WILSON(AT)ROGERS(PERIOD)COM> wrote in message news:eI*************@TK2MSFTNGP09.phx.gbl... > You'll have to "override" or "new" the inherited property and then you can > provide your own implementation, or call the "base" to allow the base > to > provide the implementation, and then provide the proper attributes, as you > have done. > > -- > Tim Wilson > .Net Compact Framework MVP > > "Michael Rodriguez" <mi**@nospamforme.com> wrote in message > news:eQ**************@TK2MSFTNGP14.phx.gbl... > I'm trying to figure out how to subclass a control and override some of > the > default properties. I can subclass a control and add my own new > properties > and set their default values. But how do you change the default value for > an *existing* property? > > For a custom property I can do this: > > [DefaultValue(FormatType.Default)] > public FormatType FormatAs > { > get { return this.formatAs; } > set { this.formatAs = value; } > } > > But when I try that code structure with an existing property it won't > compile. What am I missing? > > TIA, > > Mike Rodriguez > >
If the property already exists in the base class but is not marked as
virtual or override you should mark it as "new" to tell the compiler that
your property (in the subclass) is supposed to hide the base classes
version. At any time, from within the definition of the subclass, you may
access the base version of the property using the "base" keyword (for
example, base.Property). So only the subclasses version of the property will
be exposed, the base classes version will be hidden, but you can still
access the bases implementation from within the subclass using the "base"
keyword.
--
Tim Wilson
..Net Compact Framework MVP
"Michael Rodriguez" <mi**@nospamforme.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl... Tim,
If the base property is not marked virtual or override, and I use new,
what will that do? Will it still keep the properties of the base from which it inherits?
Thanks,
Mike
"Tim Wilson" <TIM(UNDERSCORE)WILSON(AT)ROGERS(PERIOD)COM> wrote in message news:%2******************@tk2msftngp13.phx.gbl... Yeah. If the base property is marked "virtual" or "override", then you should "override". In contrast, if the base property is not marked "virtual" or "override", then you'd use "new".
-- Tim Wilson .Net Compact Framework MVP
"Michael Rodriguez" <mi**@nospamforme.com> wrote in message news:u8*************@TK2MSFTNGP15.phx.gbl... Tim,
I got the override to work. I discovered that it works on some properties and not others, depending on how the property was originally declared.
Thanks,
Mike
"Tim Wilson" <TIM(UNDERSCORE)WILSON(AT)ROGERS(PERIOD)COM> wrote in message news:eI*************@TK2MSFTNGP09.phx.gbl... > You'll have to "override" or "new" the inherited property and then
you can > provide your own implementation, or call the "base" to allow the base > to > provide the implementation, and then provide the proper attributes,
as you > have done. > > -- > Tim Wilson > .Net Compact Framework MVP > > "Michael Rodriguez" <mi**@nospamforme.com> wrote in message > news:eQ**************@TK2MSFTNGP14.phx.gbl... > I'm trying to figure out how to subclass a control and override some
of > the > default properties. I can subclass a control and add my own new > properties > and set their default values. But how do you change the default
value for > an *existing* property? > > For a custom property I can do this: > > [DefaultValue(FormatType.Default)] > public FormatType FormatAs > { > get { return this.formatAs; } > set { this.formatAs = value; } > } > > But when I try that code structure with an existing property it won't > compile. What am I missing? > > TIA, > > Mike Rodriguez > >
Tim,
First off, thanks for all of your help. I'm still a little confused,
though. Suppose their is a property called Bold that I can't override. I
declare it as "new" and give it a default value. When the base class of
that component uses the Bold property to do the painting, will it see it's
own base class version or will it see my new implementation of it? Isn't my
new version "hidden" from the base class?
Thanks again,
Mike
"Tim Wilson" <TIM(UNDERSCORE)WILSON(AT)ROGERS(PERIOD)COM> wrote in message
news:Ow**************@TK2MSFTNGP14.phx.gbl... If the property already exists in the base class but is not marked as virtual or override you should mark it as "new" to tell the compiler that your property (in the subclass) is supposed to hide the base classes version. At any time, from within the definition of the subclass, you may access the base version of the property using the "base" keyword (for example, base.Property). So only the subclasses version of the property will be exposed, the base classes version will be hidden, but you can still access the bases implementation from within the subclass using the "base" keyword.
-- Tim Wilson .Net Compact Framework MVP
"Michael Rodriguez" <mi**@nospamforme.com> wrote in message news:%2****************@TK2MSFTNGP14.phx.gbl... Tim,
If the base property is not marked virtual or override, and I use new, what will that do? Will it still keep the properties of the base from which it inherits?
Thanks,
Mike
"Tim Wilson" <TIM(UNDERSCORE)WILSON(AT)ROGERS(PERIOD)COM> wrote in message news:%2******************@tk2msftngp13.phx.gbl... > Yeah. If the base property is marked "virtual" or "override", then you > should "override". In contrast, if the base property is not marked > "virtual" > or "override", then you'd use "new". > > -- > Tim Wilson > .Net Compact Framework MVP > > "Michael Rodriguez" <mi**@nospamforme.com> wrote in message > news:u8*************@TK2MSFTNGP15.phx.gbl... >> Tim, >> >> I got the override to work. I discovered that it works on some >> properties >> and not others, depending on how the property was originally declared. >> >> Thanks, >> >> Mike >> >> >> "Tim Wilson" <TIM(UNDERSCORE)WILSON(AT)ROGERS(PERIOD)COM> wrote in >> message >> news:eI*************@TK2MSFTNGP09.phx.gbl... >> > You'll have to "override" or "new" the inherited property and then you > can >> > provide your own implementation, or call the "base" to allow the >> > base >> > to >> > provide the implementation, and then provide the proper attributes, as > you >> > have done. >> > >> > -- >> > Tim Wilson >> > .Net Compact Framework MVP >> > >> > "Michael Rodriguez" <mi**@nospamforme.com> wrote in message >> > news:eQ**************@TK2MSFTNGP14.phx.gbl... >> > I'm trying to figure out how to subclass a control and override some of >> > the >> > default properties. I can subclass a control and add my own new >> > properties >> > and set their default values. But how do you change the default value > for >> > an *existing* property? >> > >> > For a custom property I can do this: >> > >> > [DefaultValue(FormatType.Default)] >> > public FormatType FormatAs >> > { >> > get { return this.formatAs; } >> > set { this.formatAs = value; } >> > } >> > >> > But when I try that code structure with an existing property it >> > won't >> > compile. What am I missing? >> > >> > TIA, >> > >> > Mike Rodriguez >> > >> > >> >> > >
When using the "new" keyword, the base class uses its own property
implementation and the subclass, by default, uses its own property
implementation. That is, until you qualify the property access with the
"base" keyword (for example, base.Bold), the subclass would use its own
implementation. Just as you would expect - no tricks. So let's look at an
example.
There are two classes below, A and B. B derives, or inherits, from A. A is a
superclass (base class). B is a subclass (child class).
public class A : System.Object
{
private bool bold = false;
public bool Bold
{
get
{
return bold;
}
set
{
bold = value;
}
}
public void Display()
{
MessageBox.Show(Bold.ToString());
}
}
public class B : A
{
private bool bold = true;
public new bool Bold
{
get
{
return bold;
}
set
{
bold = value;
}
}
}
If you were to compile and run the code below, what would you expect to see?
A a = new A();
a.Display();
You would see "False". Since the call to the Display() method is against an
object of type "A" and Bold holds a value of "false". Now what if we changed
the code as shown below. What would be displayed?
B b = new B();
b.Display();
"False". Since "B" simply inherits the Display() method from "A", and, since
Display() is defined in the base class, the base classes implementation of
the Bold property is used. Now what if we changed the Bold property to be
"virtual" in the base class and "override" in the derived class, as shown
below.
public class A : System.Object
{
private bool bold = false;
public virtual bool Bold
{
get
{
return bold;
}
set
{
bold = value;
}
}
public void Display()
{
MessageBox.Show(Bold.ToString());
}
}
public class B : A
{
private bool bold = true;
public override bool Bold
{
get
{
return bold;
}
set
{
bold = value;
}
}
}
Now, if you were to compile and run the code below, what would you expect to
see?
A a = new A();
a.Display();
You would still see "False". Since the call to the Display() method is still
against an object of type "A" and Bold holds a value of "false". Now what if
we changed the code as shown below. What would be displayed?
B b = new B();
b.Display();
This time you will see "True". Why? In this case, because of the base
property being "virtual" and the inheriting class property being "override",
which implementation is used when accessing the Bold property is up to the
actual type that is making the call. So since "B" is the type, the Bold
property implementation in "B" is used. To learn more about how
virtual/override is used in different situations look into "polymorphism".
So now lets, hopefully, answer your question. Suppose their is a property called Bold that I can't override. I declare it as "new" and give it a default value. When the base class of that component uses the Bold property to do the painting, will it see it's own base class version or will it see my new implementation of it? Isn't my new version "hidden" from the base class?
The base class would see its own implementation since the virtual/override
mechanism is not in place. However, you can wrap the base property as shown
in the code below.
public class B : A
{
private bool bold = true;
public new bool Bold
{
get
{
return base.Bold;
}
set
{
base.Bold = value;
}
}
public B()
{
base.Bold = bold;
}
}
Hope that helps.
--
Tim Wilson
..Net Compact Framework MVP
That did it! The part I was missing was the get/set sections with { return
base.Bold; }. Once I did it that way it worked as I had hoped for.
Thanks for all of your help, Tim.
Mike
"Tim Wilson" <TIM(UNDERSCORE)WILSON(AT)ROGERS(PERIOD)COM> wrote in message
news:eD**************@TK2MSFTNGP15.phx.gbl... When using the "new" keyword, the base class uses its own property implementation and the subclass, by default, uses its own property implementation. That is, until you qualify the property access with the "base" keyword (for example, base.Bold), the subclass would use its own implementation. Just as you would expect - no tricks. So let's look at an example.
There are two classes below, A and B. B derives, or inherits, from A. A is a superclass (base class). B is a subclass (child class).
public class A : System.Object { private bool bold = false;
public bool Bold { get { return bold; } set { bold = value; } }
public void Display() { MessageBox.Show(Bold.ToString()); } }
public class B : A { private bool bold = true;
public new bool Bold { get { return bold; } set { bold = value; } } }
If you were to compile and run the code below, what would you expect to see?
A a = new A(); a.Display();
You would see "False". Since the call to the Display() method is against an object of type "A" and Bold holds a value of "false". Now what if we changed the code as shown below. What would be displayed?
B b = new B(); b.Display();
"False". Since "B" simply inherits the Display() method from "A", and, since Display() is defined in the base class, the base classes implementation of the Bold property is used. Now what if we changed the Bold property to be "virtual" in the base class and "override" in the derived class, as shown below.
public class A : System.Object { private bool bold = false;
public virtual bool Bold { get { return bold; } set { bold = value; } }
public void Display() { MessageBox.Show(Bold.ToString()); } }
public class B : A { private bool bold = true;
public override bool Bold { get { return bold; } set { bold = value; } } }
Now, if you were to compile and run the code below, what would you expect to see?
A a = new A(); a.Display();
You would still see "False". Since the call to the Display() method is still against an object of type "A" and Bold holds a value of "false". Now what if we changed the code as shown below. What would be displayed?
B b = new B(); b.Display();
This time you will see "True". Why? In this case, because of the base property being "virtual" and the inheriting class property being "override", which implementation is used when accessing the Bold property is up to the actual type that is making the call. So since "B" is the type, the Bold property implementation in "B" is used. To learn more about how virtual/override is used in different situations look into "polymorphism".
So now lets, hopefully, answer your question.
Suppose their is a property called Bold that I can't override. I declare it as "new" and give it a default value. When the base class of that component uses the Bold property to do the painting, will it see it's own base class version or will it see my new implementation of it? Isn't my new version "hidden" from the base class?
The base class would see its own implementation since the virtual/override mechanism is not in place. However, you can wrap the base property as shown in the code below.
public class B : A { private bool bold = true;
public new bool Bold { get { return base.Bold; } set { base.Bold = value; } }
public B() { base.Bold = bold; } }
Hope that helps.
-- Tim Wilson .Net Compact Framework MVP
Glad to help.
--
Tim Wilson
..Net Compact Framework MVP
"Michael Rodriguez" <mi**@nospamforme.com> wrote in message
news:eH**************@TK2MSFTNGP09.phx.gbl... That did it! The part I was missing was the get/set sections with {
return base.Bold; }. Once I did it that way it worked as I had hoped for.
Thanks for all of your help, Tim.
Mike
"Tim Wilson" <TIM(UNDERSCORE)WILSON(AT)ROGERS(PERIOD)COM> wrote in message news:eD**************@TK2MSFTNGP15.phx.gbl... When using the "new" keyword, the base class uses its own property implementation and the subclass, by default, uses its own property implementation. That is, until you qualify the property access with the "base" keyword (for example, base.Bold), the subclass would use its own implementation. Just as you would expect - no tricks. So let's look at
an example.
There are two classes below, A and B. B derives, or inherits, from A. A
is a superclass (base class). B is a subclass (child class).
public class A : System.Object { private bool bold = false;
public bool Bold { get { return bold; } set { bold = value; } }
public void Display() { MessageBox.Show(Bold.ToString()); } }
public class B : A { private bool bold = true;
public new bool Bold { get { return bold; } set { bold = value; } } }
If you were to compile and run the code below, what would you expect to see?
A a = new A(); a.Display();
You would see "False". Since the call to the Display() method is against an object of type "A" and Bold holds a value of "false". Now what if we changed the code as shown below. What would be displayed?
B b = new B(); b.Display();
"False". Since "B" simply inherits the Display() method from "A", and, since Display() is defined in the base class, the base classes implementation
of the Bold property is used. Now what if we changed the Bold property to
be "virtual" in the base class and "override" in the derived class, as
shown below.
public class A : System.Object { private bool bold = false;
public virtual bool Bold { get { return bold; } set { bold = value; } }
public void Display() { MessageBox.Show(Bold.ToString()); } }
public class B : A { private bool bold = true;
public override bool Bold { get { return bold; } set { bold = value; } } }
Now, if you were to compile and run the code below, what would you
expect to see?
A a = new A(); a.Display();
You would still see "False". Since the call to the Display() method is still against an object of type "A" and Bold holds a value of "false". Now
what if we changed the code as shown below. What would be displayed?
B b = new B(); b.Display();
This time you will see "True". Why? In this case, because of the base property being "virtual" and the inheriting class property being "override", which implementation is used when accessing the Bold property is up to
the actual type that is making the call. So since "B" is the type, the Bold property implementation in "B" is used. To learn more about how virtual/override is used in different situations look into
"polymorphism". So now lets, hopefully, answer your question.
Suppose their is a property called Bold that I can't override. I declare it as "new" and give it a default value. When the base class of that component uses the Bold property to do the painting, will it see it's own base class version or will it see my new implementation of it? Isn't my new version "hidden" from the base class?
The base class would see its own implementation since the
virtual/override mechanism is not in place. However, you can wrap the base property as shown in the code below.
public class B : A { private bool bold = true;
public new bool Bold { get { return base.Bold; } set { base.Bold = value; } }
public B() { base.Bold = bold; } }
Hope that helps.
-- Tim Wilson .Net Compact Framework MVP
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: WhiteRavenEye |
last post by:
Why can't I subclass any window except mine in VB?
Do I have to write dll for this?
I've tried to subclass it with SetWindowLong but without success...
Does anyone know how to subclass window...
|
by: BJörn Lindqvist |
last post by:
A problem I have occured recently is that I want to subclass builtin
types. Especially subclassing list is very troublesome to me. But I
can't find the right syntax to use. Take for example this...
|
by: Brent |
last post by:
I'd like to subclass the built-in str type. For example:
--
class MyString(str):
def __init__(self, txt, data):
super(MyString,self).__init__(txt)
self.data = data
|
by: Pieter Linden |
last post by:
Hi,
This question refers sort of to Rebecca Riordan's article on Access
web about subclassing entities:
http://www.mvps.org/access/tables/tbl0013.htm
How practical is this? I am writing a...
|
by: TheStripe |
last post by:
Hello all,
Wondering if anyone has come across this problem I am having..
I am Subclassing a RadioButtonList to add some properties that I need. I am
building up
a table and adding to a...
|
by: Simon |
last post by:
I've struggled manfully and have finally managed to subclass the Common
Dialog class to add my own controls.
My last problem is how to set the lpfnHook attribute of the OPENFILENAME
structure....
|
by: Steamboat |
last post by:
I have read two excellent articles on how to subclass .NET controls so that
you can hook into the control’s WndProc, which is very helpful since you
cannot access the WndProc directly for .NET...
|
by: Its Me Ernest T. |
last post by:
I am looking for any information about how I could subcass a form I don't
own and resize some buttons.
We run a application which is extreamly legacy and the company has long
since went out of...
|
by: manatlan |
last post by:
I've got an instance of a class, ex :
b=gtk.Button()
I'd like to add methods and attributes to my instance "b".
I know it's possible by hacking "b" with setattr() methods. But i'd
like to do...
|
by: Ray |
last post by:
Hi all,
I am thinking of subclassing the standard string class so I can do
something like:
mystring str;
....
str.toLower ();
A quick search on this newsgroup has found messages by others
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |