473,549 Members | 2,689 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2007
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*********@gm ail.comwrote in message
news:11******** **************@ j44g2000cwa.goo glegroups.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*********@gm ail.comwrote in message
news:11******** **************@ j44g2000cwa.goo glegroups.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.For m = 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*********@gm ail.comwrote in message
news:11******** **************@ j44g2000cwa.goo glegroups.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.For m = 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*********@gm ail.comwrote in message
news:11******** **************@ j44g2000cwa.goo glegroups.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*********@gm ail.comwrote in message
news:11******** **************@ m73g2000cwd.goo glegroups.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.For m = 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*********@g mail.comwrote in message
news:11******* *************** @j44g2000cwa.go oglegroups.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
1536
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: try: f = file(filename, op) except IOError, e: # Handle exception else: # Use the file
17
4188
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 considering that using "find -print0" is so common, it seems like there should be a more cannonical way. |>oug
3
10009
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 has to be in canonical format or dialing rules won't be applied (cf. MSKB Article 318575). I don't want to use an Input Mask because users like to...
1
1757
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 version 2.0 of the website of the Center for CANONICAL |SCIENCE). The current preliminary version -in proof stage- has been developed on XHTML 1.1 +...
0
1651
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. http://canonicalscience.blogspot.com/2006/04/scientific-language-canonml-is.html] Some requirements fit into the XML model and could be considered for debate for the future mathML specifications. Other...
1
1727
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 XML-MAIDEN project (which provided many interesting ideas over original desing). The initial CanonMath program (was abandoned) was presented at the...
1
2530
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 Boolean functions ex:- for 2 i/p variables there are 16 different functions. then he mentions about canonical forms. he says about sum of min...
2
4811
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, Universal String: ISO/ IEC 10646, 4-octet canonical form. An excellent discussion occured with respect to BMP Strings and .Net (see...
18
6030
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 that new row should not added and presses up arrow. DataGridView does not show this unfinished row anymore.
0
7475
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7746
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7509
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7836
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5393
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5116
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3521
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3502
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
789
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.