Hi,
Oh, and you can access frmForm from obj3 by passing it a reference. This can
be done during construction or by setting a property. One or both may be used
on either obj1, since it creates obj3, but at least one must be used on obj3.
This example uses only a single property on the obj3 class, which is assigned
a reference to the frmForm instance by the frmForm instance itself:
class frmForm
{
public frmForm()
{
obj1 obj = new obj1();
obj.Object3.Form = this;
}
}
class obj1
{
public Object3 { get { return object3; } }
private obj3 object3 = new obj3();
}
class obj3
{
public frmForm Form { get { return form; } set { form = value; } }
private frmForm form;
}
Sometimes it makes more sense to use a constructor argument (useful for
immutability):
class frmForm
{
public frmForm()
{
obj1 obj = new obj1(this);
}
}
class obj1
{
public Object3 { get { return object3; } }
private obj3 object3;
public obj1(frmForm form)
{
// forward the form reference to the new obj3 object.
object3 = new obj3(form);
}
}
class obj3
{
// readonly keyword prevents write access to this field outside of the
constructor
// (though reflection may be used to assign a different value).
private readonly frmForm form;
public obj3(frmForm form)
{
this.form = form;
}
}
The canonical way (if there is one) depends on your specific requirements,
which you haven't stated.
(Sorry about the 3 separate answers here. If I still missed anything let me
know ;)
--
Dave Sexton
<wp*********@gmail.comwrote in message
news:11**********************@j44g2000cwa.googlegr oups.com...
What is the cleanest way to gain access to object methods and
properties across classes and files in the same namespace?
Example: A form object frmForm in file frmForm.cs creates obj1 defined
in file obj1.cs, which in turn creates obj2 and obj3 defined in obj2.cs
and obj3.cs, respectively.
What is the canonical way for frmForm to access obj2 and obj3? For obj3
to access frmForm?