Re: set a property of inherited class from base class

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: hOSAM (hosam.shahin$$gmail.com)
Date: 09/22/04


Date: Thu, 23 Sep 2004 02:57:14 +0300

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" <manofsteele@311.com> wrote in message
news:##wb5kPoEHA.3252@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).Left = 44;
>
>
>
>



Relevant Pages

  • Stylistic question about inheritance
    ... We might imagine various kinds of expressions, ... If I were solving such a problem in C++, I would define a base class for all ... then derive the various kinds of expression classes from that ... there are reasons to have a base class anyway. ...
    (comp.lang.python)
  • Re: Changing return of type(obj)
    ... the base class on an instance by instance basis depending on the need? ... now I can imagine having a class factory that will spawn for me the class ...
    (comp.lang.python)
  • Re: set a property of inherited class from base class
    ... > I am trying to get set a property of a control on the inherited class from ... I imagine i have to use reflection, ... of an instance of a inherited class from an instance of the base class then ...
    (microsoft.public.dotnet.languages.csharp)