canonical object access

  • Thread starter Thread starter wpmccormick
  • Start date Start date
W

wpmccormick

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?
 
Hi,

If the classes are in the same namespace then you won't have to do anything
special at all. The compiler will resolve all unambiguous references without
your help. The fact that the classes are in different files is irrelevant.

In C# 2.0, partial classes are commonly used. Usually, you'd split partial
classes into multiple files.

When the objects are in different namespaces, add a "using" statement to the
top of your .cs file.
 
Hi,

Ok, so it seems upon further review of your question that namespaces are
completely irrelevant :)

What you need is access modifiers (The source files have nothing to do with
this).

// the "public" modifier makes this class visible to all other classes and
assemblies
public class frmForm : Form
{
public frmForm() // public = constructor access modifier
{
obj1 obj = new obj1();
obj2 = obj.Object2;
}
}

// this class has, by default, "internal" access because no modifier was
specified
class obj1
{
// the "public" modifier makes this property visible to all other classes
and assemblies
public Object2
{
get
{
return object2;
}
}

// the "private" modifier, which does not need to be specified (but I
prefer to specify it)
// hides this "field" from all public access. Only this class and nested
classes can
// access the property without using reflection.
private obj2 object2 = new obj2();
}
 
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 said:
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 ;)

Thanks Dave!

I don't have any specific requirements other NOT creating a mess.
 
Hi,

Ok, then you should try to make immutability a requirement and use the
constructor approach. This way, the state of the objects can't be changed
after construction (without reflection).

Immutability prevents unwanted and unexpected changes to the state of an
object, which otherwise may create bugs that are somewhat hard to identify and
fix unless you have extremely verbose tracing code in place.
 
Back
Top