Overriding Inherited Propeties

G

Guest

I have a base class for a label. I want to set a property at the base class
level, say AutoSize = true. Then at the form level I want to be able to
overide the base class setting so that I can turn it off when needed.

What is happening is that at the form level, I can set the autosize to
false, but then when I recompile, the autosize is set back to true.

This is what the base class looks like:

public class LabelBase : Label

public LabelBase()
{
this.AutoSize = true ;
}

Is there some other way that I should be doing this so that I can override
it at the form level.
 
T

Tim Wilson

I would say override the AutoSize property and specify the
DefaultValueAttribute as "true".

[DefaultValue(true)]
public override bool AutoSize
{
get
{
return base.AutoSize;
}
set
{
base.AutoSize = value;
}
}

public LabelBase()
{
this.AutoSize = true;
}
 
G

Guest

Sorry, no luck with doing this. It seems odd to me that I can set a property
in the property sheet of a form, and that it reverts on saving the form. It
seems like such a simple thing to me to be able to override a base class
property.

Tim Wilson said:
I would say override the AutoSize property and specify the
DefaultValueAttribute as "true".

[DefaultValue(true)]
public override bool AutoSize
{
get
{
return base.AutoSize;
}
set
{
base.AutoSize = value;
}
}

public LabelBase()
{
this.AutoSize = true;
}

--
Tim Wilson
..Net Compact Framework MVP

Greg said:
I have a base class for a label. I want to set a property at the base class
level, say AutoSize = true. Then at the form level I want to be able to
overide the base class setting so that I can turn it off when needed.

What is happening is that at the form level, I can set the autosize to
false, but then when I recompile, the autosize is set back to true.

This is what the base class looks like:

public class LabelBase : Label

public LabelBase()
{
this.AutoSize = true ;
}

Is there some other way that I should be doing this so that I can override
it at the form level.
 
B

Bruce Wood

I do this in many places, so there is something else at foot. Can you
post the complete code for your derived LabelBase, and the code in your
form that initializes it?
 
B

Bob Grommes

Greg,

Are you sure that there is not some code you've written into LabelBase that
is executing unexpectedly at design time?

You may have to make some code conditional to run only at runtime. It may
even just be that your constructor is getting called and resetting AutoSize
back to true.

Also, as an aside, shouldn't you be calling the base constructor and then
setting AutoSize to true? You typically want to augment the base
constructor, not completely override it. You don't know what initialization
the base class is doing, and you might be creating unexpected behaviors by
preventing the base class constructor from running.

--Bob

Greg said:
Sorry, no luck with doing this. It seems odd to me that I can set a
property
in the property sheet of a form, and that it reverts on saving the form.
It
seems like such a simple thing to me to be able to override a base class
property.

Tim Wilson said:
I would say override the AutoSize property and specify the
DefaultValueAttribute as "true".

[DefaultValue(true)]
public override bool AutoSize
{
get
{
return base.AutoSize;
}
set
{
base.AutoSize = value;
}
}

public LabelBase()
{
this.AutoSize = true;
}

--
Tim Wilson
..Net Compact Framework MVP

Greg said:
I have a base class for a label. I want to set a property at the base class
level, say AutoSize = true. Then at the form level I want to be able
to
overide the base class setting so that I can turn it off when needed.

What is happening is that at the form level, I can set the autosize to
false, but then when I recompile, the autosize is set back to true.

This is what the base class looks like:

public class LabelBase : Label

public LabelBase()
{
this.AutoSize = true ;
}

Is there some other way that I should be doing this so that I can
override
it at the form level.
 
T

Tim Wilson

Well, my thinking on this problem was along these lines...

You want to set the property (AutoSize) of the base class to a different
value than its default. The base class AutoSize property has a
DefaultValueAttribute with a value of "false". So you set a different value
("true") into the base AutoSize property in your derived controls
constructor. Now, that property should be serialized into the
InitializeComponent method ("this.labelBase1.AutoSize = true;") since it
does not match the default value ("false"). All is well as this point. Now,
when you change the value to "false", there is no need to serialize the
value as it matches the bases default AutoSize value. The appropriate line
in the InitializeComponent method will be removed. For the duration that you
have the Form open in the designer, the control instance is storing the
value of "false". When you run the code, or when you close the Form and
reopen it in the designer, the bases AutoSize is again set in the
constructor to "true" and the value is not set to "false" in the
InitializeComponent method as you would think due to what was mentioned
previously - the line "this.labelBase1.AutoSize = false;" has been removed
since the control believes it is not necessary.

So the code that I posted will override the AutoSize property, still using
the bases implementation through the getter and setter, but it changes the
default value, through the DefaultValueAttribute, to line up with the
desired default. Not only would this give a better design experience, but
when the value is changed to "false" it will be serialized.

Are you sure that you have overriden the AutoSize in your class and
specified the DefaultValueAttribute?

--
Tim Wilson
..Net Compact Framework MVP

Greg said:
Sorry, no luck with doing this. It seems odd to me that I can set a property
in the property sheet of a form, and that it reverts on saving the form. It
seems like such a simple thing to me to be able to override a base class
property.

Tim Wilson said:
I would say override the AutoSize property and specify the
DefaultValueAttribute as "true".

[DefaultValue(true)]
public override bool AutoSize
{
get
{
return base.AutoSize;
}
set
{
base.AutoSize = value;
}
}

public LabelBase()
{
this.AutoSize = true;
}

--
Tim Wilson
..Net Compact Framework MVP

Greg said:
I have a base class for a label. I want to set a property at the base class
level, say AutoSize = true. Then at the form level I want to be able to
overide the base class setting so that I can turn it off when needed.

What is happening is that at the form level, I can set the autosize to
false, but then when I recompile, the autosize is set back to true.

This is what the base class looks like:

public class LabelBase : Label

public LabelBase()
{
this.AutoSize = true ;
}

Is there some other way that I should be doing this so that I can override
it at the form level.
 
J

Jon Skeet [C# MVP]

Bob Grommes said:
Are you sure that there is not some code you've written into LabelBase that
is executing unexpectedly at design time?

You may have to make some code conditional to run only at runtime. It may
even just be that your constructor is getting called and resetting AutoSize
back to true.

Also, as an aside, shouldn't you be calling the base constructor and then
setting AutoSize to true? You typically want to augment the base
constructor, not completely override it. You don't know what initialization
the base class is doing, and you might be creating unexpected behaviors by
preventing the base class constructor from running.

The base class constructor is running already. If neither this(...) or
base(...) is specified, there's an implicit call to the parameterless
base constructor.

However, my guess is that the instance is being created and then the
designer code is changing the value.
 
G

Guest

Tim,

I went back and recreated the problem with a single form and single label
with the orginal baseclass, and the problem was apparent. However, once I
plugged your code in, it did indeed resolve this.

Thank you very much for the help.

Greg
 

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