471,594 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,594 software developers and data experts.

canonical object access

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?

Nov 17 '06 #1
5 1909
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.

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

Nov 17 '06 #2
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();
}

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

Nov 17 '06 #3
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?

Nov 17 '06 #4

Dave Sexton wrote:
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?
Thanks Dave!

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

Nov 17 '06 #5
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.

--
Dave Sexton

<wp*********@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
>
Dave Sexton wrote:
>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.googleg roups.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?

Thanks Dave!

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

Nov 17 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Hans-Joachim Widmaier | last post: by
17 posts views Thread by Douglas Alan | last post: by
1 post views Thread by zzz | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.