Subclassing controls

M

Michael Rodriguez

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
 
T

Tim Wilson

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

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
 
M

marc.derider

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(...);
}
 
T

Tim Wilson

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.
 
M

Michael Rodriguez

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
 
T

Tim Wilson

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 said:
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 said:
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

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
 
M

Michael 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 said:
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 said:
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
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

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
 
T

Tim Wilson

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 said:
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 said:
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 said:
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
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

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
 
M

Michael 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 said:
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 said:
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
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

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
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

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
 
T

Tim Wilson

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.
 
M

Michael Rodriguez

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top