OK for CustomControl to have Class Property that Knows It Control?

It is complicated to capture this question in a title.

But my real question is relating to a custom canvas control that has a class as one of its (the canvas) properties. Now that class has some properties that I want to update. But the calculation that does the updating needs access to properties of the custom canvas control that contains it.

It seems convoluted which bothers me, but is it kosher to have a class, that is one of the canvas’s properties, have a property itself which is basically the instance of the custom canvas that contains it?

So the Constructor event of the custom canvas would create an instance of the class that is one of its properties and then assign itself to one of the properties of the class that is being created. So in the future, that class in its methods could reference properties or methods of the canvas instance in which it is imbedded as a property.

The design is tightly coupled. Sometimes there is no alternative.

I prefer to use containers so that everything that belongs to a canvas is on the container. There the container is the natural owner of everything and can do initialisation. It really depends on how complex your canvas/class combo is.

[quote=486413:@Robert Livingston]It is complicated to capture this question in a title.

But my real question is relating to a custom canvas control that has a class as one of its (the canvas) properties. Now that class has some properties that I want to update. But the calculation that does the updating needs access to properties of the custom canvas control that contains it.

It seems convoluted which bothers me, but is it kosher to have a class, that is one of the canvas’s properties, have a property itself which is basically the instance of the custom canvas that contains it?

So the Constructor event of the custom canvas would create an instance of the class that is one of its properties and then assign itself to one of the properties of the class that is being created. So in the future, that class in its methods could reference properties or methods of the canvas instance in which it is imbedded as a property.[/quote]
There are times when this is unavoidable, but this is a great way to create a memory leak if you are not careful.

I suggest the following:

  1. On the class, make a property that is of the parent type.
  2. Right-click the property and select Convert to Computed Property.
  3. Change the backing property type to WeakRef
  4. Change the Setter code to: ... = New WeakRef(value)
  5. In the getter code, something like this:

// assuming the property is named Parent // and the Type is CustomCanvas If mParent = nil or mParent.Value = Nil Then Return Nil End If Return CustomClass(mParent.Value)

Now anywhere you use it within this class, just make sure you check for Nil before using it. To simplify that, assign it to a local property at the top of any method to temporarily hold a reference and then check for nil.

You can use events to communicate data back and forth
Sometimes thats a better design
Its on my blog under “Using events as a way to pass information to instances”