Exposing properties

Hi!

My idea is to include into language a mechanism to expose a field's properties. Let's say I'm inside some WinForms user controll. My control's got a label named label1. I want to expose the Text proprety of the label inside my user controll class and call it LabelText. The main pattern idea is to 'inherit' all the child's property attributes automaticly (like DefaultValue, Browsable and so on - these are just examples, Text proprety's got some others). I would only have to write single line of code that would do everything for me, like

public LabelText = label1.Text;

One problem I've noticed is that some of the attributes may be of private nested inside that class types, like

class MyClass
{
[AttributeUsage(AttributeTargets.All)]
private class MyAttribute : DescriptionAttribute
{
public MyAttribute(string description)
: base(description)
{
}
}
[MyAttribute("my attribute that visualizes the problem")]
public int X = 0;
}

Cheers,
Dawid Ireno.



Answer this question

Exposing properties

  • mertkan65

    You're a good magician Joel
  • Ramesh Nikam

    This is not possible. C# has some rules which it doesnot let anyone to break unlike VB6 or ASP where when adding to number like 1 + 1 becomes 11 and MSGBOX shows everything whether its String, int etc which leads to an application which is prone to Logical Errors when deployed in some commmercial Environment.

    Best Regards,

    Rizwan aka RizwanSharp



  • SimonOng

    Hi,

    What you exactly wanted Can you be more specific

    Thanks



  • Puneet Minda

    Oh, and by the way, because of that, when I design a UserControl, I always declare its components as "protected", and only go to "private" if I have a good reason. That way, I won't be bugged if/when I decide to reuse them. I mean, this is OOP, after all .

    EDIT: silly me, I wrote "internal" when I meant "protected". Hopefully, you have caught that up


  • CJW99

    I'm not sure I understand you problem right. However, here's some help.

    I created a UserControl.ascx (under VWD 2005), put a Literal in it, and wrote a public method which takes a string as an argument and writes is to the Literal

    public void WriteToPage (string message) {
    Literal1.Text += message + "<br/>";
    }


    I added this control the WebForm.aspx.
    Then, from the WebForm.aspx I wrote a line of code which calls that method.

    this.UserControl1.WriteToPage("this is a test");

    The text was displayed. Is this what you wanted If so, I guess that all it takes is to write properties like you normally would do, and access them from the parent form.


    As for accessing private members... it's part of the language specifications, that you must not be able to do so.



    Hope this helps.
    And ask further if you have any questions.


  • Jawad Naeem

    Well.. yes and no :-) This is sth. about what I wanted to - so YES, but private - NO. In my vision public properties of children would be only exposed as public or protected, and protected properties would be exposed as protected. It's an almost fine solution, am I right, or right ;-)

    So if Text property in Label would be let's say "public Text" my line might be

    public LabelText = label1.Text;

    or

    protected LabelText = label1.Text;

    and if Text property in Label would be "protected Text" my line might be only

    protected LabelText = label1.Text;

     

    Cheers, David


  • TheViewMaster

    I think I know what you're trying to do.
    I tried a bit, and indeed it's not possible.

    I guess there's only one way to do it : explicitly write a property, for each component you want to access.

    WebForm:
    this.MyUserControl1.MyLiteral1.Text = "Parent changed the text. B-)";


    UserControl1:
    public Literal MyLiteral1 {
        get {
            return Literal1;
        }
    }


  • Exposing properties