Having a reference to a property, Is there a way to grab a reference to the encompassing object?

Ahmad

I have extended the Label class as follows:

public class MyLabel: Label {
    public Button btn;
    public string mydata;
}

In my main program, I instantiated a new instance:

MyLabel lbl = new MyLabel();
lbl.mydata = "some data here";
lbl.btn = new Button();
lbl.btn.Click += new EventHandler(button_pressed);

this.Controls.Add(lbl); // Adds the label to the form
this.Controls.Add(lbl.btn); // Adds the button to the form

And I created a method to handle the button click event:

void button_pressed(Object sender, EventArgs e) {
     Button btn = (Button)sender;
     //Now I have an access to the property within MyLabel instance.
     // but how can I access the parent object?
     // I need to access the sibling property [mydata] string from here

     btn.Siblings["mydata"] = "some other thing" ;  //Something like this

     MyLabel lbl = btn.ParentObject();   //Or something like this
     lbl.mydata = "Some other thing";

}
CoolBots

This looks like WinForms, in which case either a UserControl or extending Button class might be a good way to go - just maintain a reference to the parent (a bit more complicated with UserControl, you'd need to define the click event on that control, otherwise you're back to "square 1") I like the Tag property solution as well, although there is an additional cast, and no guarantee of type safety (since Tag is an object, it can be anything by the time you try to access it).

However, let's say you're looking for a more general solution; let's also say that the class in question is sealed, has no Tag or similar purpose property, and a Controls collection is not available (or looping through it is not desirable for performance reasons). To my best knowledge, you can't determine parent object; but you can easily provide your own "Controls" style dictionary, mapping the Button to the parent:

public class MyLabel: Label {
    public static Dictionary<Button, MyLabel> ParentMap = new Dictionary<Button, MyLabel>();

    public Button btn;
    public string mydata;

    public void AddToParentMap() => ParentMap[btn] = this;
}

When you're creating an instance of MyLabel, just call the AddToParentMap() function (can't be done in constructor, because this pointer is not available until the object is created):

MyLabel lbl = new MyLabel();
lbl.AddToParentMap();

You can then just look it up, fast and easy, in your click event:

void button_pressed(Object sender, EventArgs e) {
    Button btn = (Button)sender;
    var label = MyLabel.ParentMap[btn];

    //...
    //Your code...
}

Unlike the Tag solution, type safety is guaranteed - you always know you're accessing a MyLabel object.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Reference behavior for property of an object

From Dev

Is there a way to set a variable in JavaScript equal to the property of object as a reference?

From Dev

Javascript create reference to an object property?

From Dev

Store a reference to a property of an object in a variable

From Dev

Reference error "object property is not defined"

From Dev

Store a reference to a property of an object in a variable

From Dev

Reference object property through variable

From Dev

Is there a way to pass in a ofstream reference to a object?

From Dev

_.difference checks for object reference or checks property by property?

From Dev

"Object reference not set to an instance of an object" on navigation property

From Dev

Any way to assign a variable (not an object property) another variable's value by reference in JavaScript?

From Dev

How to dynamically reference an object property in VBA

From Dev

How to dynamically reference an object property in VBA

From Dev

Local variable reference to object property overhead in JavaScript

From Dev

reference a DOM element as a property of the window object

From Dev

Object reference is required for the nonstatic field method or property

From Dev

Grab a property of an object

From Dev

Way to avoid CoffeeScript passing object reference style

From Dev

Most efficient way to reference nested object

From Dev

Most efficient way to reference nested object

From Dev

Clean way to catch error Object reference not set

From Dev

Reference to object

From Dev

Xamarin Forms Nullable Property - Object reference not set to an instance of an object

From Dev

How to reference an object property when defining an object literal in Javascript?

From Dev

C# Object reference to another object's property

From Dev

Null is of reference type, is it a String reference or Object reference?

From Dev

Is there a type-safe way to reference a class/property in an attribute?

From Dev

Is there a way to copy Date object into another date Object without using a reference?

From Dev

Can an immediately invoked property reference the object to which it is assigned?

Related Related

  1. 1

    Reference behavior for property of an object

  2. 2

    Is there a way to set a variable in JavaScript equal to the property of object as a reference?

  3. 3

    Javascript create reference to an object property?

  4. 4

    Store a reference to a property of an object in a variable

  5. 5

    Reference error "object property is not defined"

  6. 6

    Store a reference to a property of an object in a variable

  7. 7

    Reference object property through variable

  8. 8

    Is there a way to pass in a ofstream reference to a object?

  9. 9

    _.difference checks for object reference or checks property by property?

  10. 10

    "Object reference not set to an instance of an object" on navigation property

  11. 11

    Any way to assign a variable (not an object property) another variable's value by reference in JavaScript?

  12. 12

    How to dynamically reference an object property in VBA

  13. 13

    How to dynamically reference an object property in VBA

  14. 14

    Local variable reference to object property overhead in JavaScript

  15. 15

    reference a DOM element as a property of the window object

  16. 16

    Object reference is required for the nonstatic field method or property

  17. 17

    Grab a property of an object

  18. 18

    Way to avoid CoffeeScript passing object reference style

  19. 19

    Most efficient way to reference nested object

  20. 20

    Most efficient way to reference nested object

  21. 21

    Clean way to catch error Object reference not set

  22. 22

    Reference to object

  23. 23

    Xamarin Forms Nullable Property - Object reference not set to an instance of an object

  24. 24

    How to reference an object property when defining an object literal in Javascript?

  25. 25

    C# Object reference to another object's property

  26. 26

    Null is of reference type, is it a String reference or Object reference?

  27. 27

    Is there a type-safe way to reference a class/property in an attribute?

  28. 28

    Is there a way to copy Date object into another date Object without using a reference?

  29. 29

    Can an immediately invoked property reference the object to which it is assigned?

HotTag

Archive