473,320 Members | 1,802 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Overriding Inherited Propeties

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.
--
Greg Gum
Nov 17 '05 #1
7 1424
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" <Gr**@discussions.microsoft.com> wrote in message
news:E4**********************************@microsof t.com...
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.
--
Greg Gum

Nov 17 '05 #2
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" wrote:
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" <Gr**@discussions.microsoft.com> wrote in message
news:E4**********************************@microsof t.com...
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.
--
Greg Gum


Nov 17 '05 #3
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?

Nov 17 '05 #4
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" <Gr**@discussions.microsoft.com> wrote in message
news:A8**********************************@microsof t.com...
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" wrote:
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" <Gr**@discussions.microsoft.com> wrote in message
news:E4**********************************@microsof t.com...
> 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.
> --
> Greg Gum


Nov 17 '05 #5
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" <Gr**@discussions.microsoft.com> wrote in message
news:A8**********************************@microsof t.com...
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" wrote:
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" <Gr**@discussions.microsoft.com> wrote in message
news:E4**********************************@microsof t.com...
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.
--
Greg Gum


Nov 17 '05 #6
Bob Grommes <bo*@bobgrommes.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #7
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

"Tim Wilson" wrote:
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


Nov 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Edward Diener | last post by:
Is it possible for a derived class to override a property and/or event of its base class ?
0
by: Ian | last post by:
I've got this problem with overriding attributes on an inheritance chain where the XmlSerializer is concerned. For example: public class A { private string aWord = String.Empty(); public...
1
by: Robert | last post by:
Hi, I've inherited the XmlDocument class to include some custom methods that I want to use on a particular XML file. I need to know whether the document has changed since being loaded, and I...
1
by: Anders Borum | last post by:
Hello! I have a framework where all objects are uniquely identified by a GUID (Global Unique Identifier). The objects are used in conjunction with lots of hashtables and I was thinking, that...
4
by: TS | last post by:
i have a class that i'm trying to understand that overrides BaseApplicationException's methods as follows. What i dont' understand is that i have never seen the inherit ":" on a method signature,...
17
by: Bob Weiner | last post by:
What is the purpose of hiding intead of overriding a method? I have googled the question but haven't found anything that makes any sense of it. In the code below, the only difference is that...
1
by: ghobley | last post by:
Hello, I have a Parent form call say MyProjectForm which is inherited, in this I have a variable call MyProjectItemNode of type treenode. I also have an overridable function called SelectNode in...
12
by: Robert W. | last post by:
This question concerns something I'm trying to do with the CF but it's really a generic C# question. With the Compact Framework, one can't add a radio button with a long label because the labels...
8
by: yashwant pinge | last post by:
#include<iostream> using namespace std; class base { public: void display() { } };
10
by: r035198x | last post by:
The Object class has five non final methods namely equals, hashCode, toString, clone, and finalize. These were designed to be overridden according to specific general contracts. Other classes that...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.