set a property of inherited class from base class

  • Thread starter Thread starter TS
  • Start date Start date
T

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).Left = 44;
 
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 said:
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).Left = 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
 
TS said:
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).Left = 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.
 
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.
 
so what if i have a method on an inherited class that is only used in this
class and not in all inherited classes?
 
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 said:
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.


me
 
thanks james

James Curran said:
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 said:
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.


class
from
give
 
Back
Top