469,903 Members | 1,806 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,903 developers. It's quick & easy.

set a property of inherited class from base class

TS
I am trying to get set a property of a control on the inherited class from
base class. I imagine i have to use reflection, so could someone give me the
code to do it?

something like this?
this.GetType().GetMember("panel1").SetValue(xx).Le ft = 44;


Nov 16 '05 #1
8 2760
Try this;
this.GetType().GetProperty("panel1").SetValue(this , 44, null);

Where the property name is "panel1", and the value is 44.

Anyways, I don't think this is good. What you are trying to do aggressively
violates encapsulation. In100% of cases there are better ways to do things
like this using OOP paradigms.
For example, you can have that property virtual/abstract on the base class,
so that the inherters implement it, and you can directly modify it without
the reflection overhead.

"TS" <ma*********@311.com> wrote in message
news:##**************@TK2MSFTNGP11.phx.gbl...
I am trying to get set a property of a control on the inherited class from
base class. I imagine i have to use reflection, so could someone give me the code to do it?

something like this?
this.GetType().GetMember("panel1").SetValue(xx).Le ft = 44;

Nov 16 '05 #2
TS wrote:
I am trying to get set a property of a control on the inherited class from
base class. I imagine i have to use reflection, so could someone give me the
code to do it?

something like this?
this.GetType().GetMember("panel1").SetValue(xx).Le ft = 44;


Is panel1 a member of the base class or of the inherited class? Could you
give a basic layout of hte two classes (you don't have to paste 400 lines,
just the idea) so we can suggest the proper solution? Reflection is almost
never the solution with these kind of problems :)

FB

--
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET Blog: http://weblogs.asp.net/fbouma
Microsoft C# MVP
Nov 16 '05 #3

"TS" <ma*********@311.com> wrote in message
news:%2******************@TK2MSFTNGP11.phx.gbl...
I am trying to get set a property of a control on the inherited class from
base class. I imagine i have to use reflection, so could someone give me the code to do it?

something like this?
this.GetType().GetMember("panel1").SetValue(xx).Le ft = 44;


There has to be a better way to do whatever it is you want to achieve.

Firstly do you mean an instance of the class? If you want to set a property
of an instance of a inherited class from an instance of the base class then
you simply send it (the inherited instance) a message from the base class
instance.

If no suitable method exists that allows you to do this then you write one.
Nov 16 '05 #4
TS
member of inherited class

I have a panel in the inherited class that i want to center in the form. I
have this panel in all the classes that inherit from base class. I wanted to
do the centering in the base class instead of on every inherited class.

i thought about making the panel on the base class, but i didn't want to
mess up my existing layout on the form becuase i was adding this panel after
the form was built and i had to add each control to the panel separately in
design mode to keep their same positions.
"Frans Bouma [C# MVP]" <pe******************@xs4all.nl> wrote in message
news:xn***************@msnews.microsoft.com...
TS wrote:
I am trying to get set a property of a control on the inherited class from base class. I imagine i have to use reflection, so could someone give me the code to do it?

something like this?
this.GetType().GetMember("panel1").SetValue(xx).Le ft = 44;
Is panel1 a member of the base class or of the inherited class? Could

you give a basic layout of hte two classes (you don't have to paste 400 lines,
just the idea) so we can suggest the proper solution? Reflection is almost
never the solution with these kind of problems :)

FB

--
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET Blog: http://weblogs.asp.net/fbouma
Microsoft C# MVP

Nov 16 '05 #5
TS
so what if i have a method on an inherited class that is only used in this
class and not in all inherited classes?

"hOSAM" <hosam.shahin$$gmail.com> wrote in message
news:Oh**************@TK2MSFTNGP14.phx.gbl...
Try this;
this.GetType().GetProperty("panel1").SetValue(this , 44, null);

Where the property name is "panel1", and the value is 44.

Anyways, I don't think this is good. What you are trying to do aggressively violates encapsulation. In100% of cases there are better ways to do things
like this using OOP paradigms.
For example, you can have that property virtual/abstract on the base class, so that the inherters implement it, and you can directly modify it without
the reflection overhead.

"TS" <ma*********@311.com> wrote in message
news:##**************@TK2MSFTNGP11.phx.gbl...
I am trying to get set a property of a control on the inherited class from base class. I imagine i have to use reflection, so could someone give me

the
code to do it?

something like this?
this.GetType().GetMember("panel1").SetValue(xx).Le ft = 44;


Nov 16 '05 #6
TS
you mean like a property accessor? can you give me an example?

"Kavvy" <bl*@debla.bla> wrote in message
news:bd*************@text.news.blueyonder.co.uk...

"TS" <ma*********@311.com> wrote in message
news:%2******************@TK2MSFTNGP11.phx.gbl...
I am trying to get set a property of a control on the inherited class from base class. I imagine i have to use reflection, so could someone give me the
code to do it?

something like this?
this.GetType().GetMember("panel1").SetValue(xx).Le ft = 44;


There has to be a better way to do whatever it is you want to achieve.

Firstly do you mean an instance of the class? If you want to set a

property of an instance of a inherited class from an instance of the base class then you simply send it (the inherited instance) a message from the base class
instance.

If no suitable method exists that allows you to do this then you write one.

Nov 16 '05 #7
What triggers the centering action?

If it's done in the base class, sometimes you'll attempt to center a
panel which is not there.

If it starts in the derived class, then class a base class method,
passing a reference to the panel as a parameter.

Or, alternately, in the base class, search it's Controls collection for
the panel.

--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
"TS" <ma*********@311.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
member of inherited class

I have a panel in the inherited class that i want to center in the form. I
have this panel in all the classes that inherit from base class. I wanted to do the centering in the base class instead of on every inherited class.

i thought about making the panel on the base class, but i didn't want to
mess up my existing layout on the form becuase i was adding this panel after the form was built and i had to add each control to the panel separately in design mode to keep their same positions.
"Frans Bouma [C# MVP]" <pe******************@xs4all.nl> wrote in message
news:xn***************@msnews.microsoft.com...
TS wrote:
I am trying to get set a property of a control on the inherited class from base class. I imagine i have to use reflection, so could someone give
me
the code to do it?

something like this?
this.GetType().GetMember("panel1").SetValue(xx).Le ft = 44;


Is panel1 a member of the base class or of the inherited class? Could

you
give a basic layout of hte two classes (you don't have to paste 400 lines, just the idea) so we can suggest the proper solution? Reflection is almost never the solution with these kind of problems :)

FB

--
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET Blog: http://weblogs.asp.net/fbouma
Microsoft C# MVP


Nov 16 '05 #8
TS
thanks james

"James Curran" <Ja*********@mvps.org> wrote in message
news:ub**************@TK2MSFTNGP12.phx.gbl...
What triggers the centering action?

If it's done in the base class, sometimes you'll attempt to center a
panel which is not there.

If it starts in the derived class, then class a base class method,
passing a reference to the panel as a parameter.

Or, alternately, in the base class, search it's Controls collection for the panel.

--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
"TS" <ma*********@311.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
member of inherited class

I have a panel in the inherited class that i want to center in the form. I
have this panel in all the classes that inherit from base class. I wanted
to
do the centering in the base class instead of on every inherited class.

i thought about making the panel on the base class, but i didn't want to
mess up my existing layout on the form becuase i was adding this panel after
the form was built and i had to add each control to the panel separately

in
design mode to keep their same positions.
"Frans Bouma [C# MVP]" <pe******************@xs4all.nl> wrote in message
news:xn***************@msnews.microsoft.com...
TS wrote:

> I am trying to get set a property of a control on the inherited class from
> base class. I imagine i have to use reflection, so could someone
give me
the
> code to do it?
>
> something like this?
> this.GetType().GetMember("panel1").SetValue(xx).Le ft = 44;

Is panel1 a member of the base class or of the inherited class?

Could you
give a basic layout of hte two classes (you don't have to paste 400

lines, just the idea) so we can suggest the proper solution? Reflection is almost never the solution with these kind of problems :)

FB

--
Get LLBLGen Pro, productive O/R mapping for .NET:

http://www.llblgen.com My .NET Blog: http://weblogs.asp.net/fbouma
Microsoft C# MVP



Nov 16 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Joăo Santa Bárbara | last post: by
10 posts views Thread by Jacob | last post: by
3 posts views Thread by Wayne Brantley | last post: by
14 posts views Thread by Dom | last post: by
19 posts views Thread by jan.loucka | last post: by
6 posts views Thread by =?Utf-8?B?SmF5IFBvbmR5?= | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.