473,394 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 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 1997
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Hans-Joachim Widmaier | last post by:
Recently, there was mentioned how someone who had understood Python's error handling would write the "open and read file with error handling" idiom. If I remember correctly, it went like this: ...
17
by: Douglas Alan | last post by:
Is there a canonical way of iterating over the lines of a file that are null-separated rather than newline-separated? Sure, I can implement my own iterator using read() and split(), etc., but...
3
by: deko | last post by:
I have a (Access 2003) contact management database where the user can double-click a contact's phone number in a form and have the Windows Phone Dialer dial the number. The problem is the number...
1
by: Juan R. | last post by:
Introduction I am developing the CanonML language (version 1.0) as a way to generate, store, and publish canonical science documents on the Internet. This language will be the basis for the next...
0
by: Juan R. | last post by:
I have updated some basic requirements for a generic mathematical markup language for scientific requirements at the next link. ...
1
by: Juan R. | last post by:
The initial CanonMath program presented here http://canonicalscience.blogspot.com/2006/02/choosing-notationsyntax-for-canonmath.html] was discussed with several specialists, including father of...
1
by: zzz | last post by:
Hi all, I was recently reading the book "Write Great code by ryndall Hyde" in this in chapter 8 the following are given. given n input variables there are two raised to two raised to n unique...
2
by: Jeffrey Walton | last post by:
Hi All, BMP Strings are a subset of Universal Strings.The BMP string uses approximately 65,000 code points from Universal String encoding. BMP Strings: ISO/IEC 10646, 2-octet canonical form,...
18
by: Andrus | last post by:
Marc, Thank you very much. I have issue on implementing add row properly using this. User presses down arrow in last row in grid starting adding new row. Then user changes its mind desiding...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.