473,326 Members | 2,732 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,326 software developers and data experts.

Accessing classB from ClassA property. (Sort of Nested Class)

Hi,

What i'm trying to do is assign a class (i.e. UKPostCode) to a propety of
the UKAddress Class.

So for example:

class UKAddress
{
public string AddressLine1
{
get { ... }
set { ... }
}
....etc.

public UKPostCode PostCode
{
get { .... }
set { .... }
}
.....etc.
}

class UKPostCode
{ ....
public void AMethod() { ... } ... etc.
}
So i can do the following:

UKAddress address = new UKAddress();

address.PostCode.AMethod().

I know this can be done if i create a public field in the class as
UKPostCode, but that does not give may any checking with the set { ... }.

Thankyou in advance,

Paul R


Jul 5 '07 #1
5 1470
On Wed, 04 Jul 2007 17:55:21 -0700, Paul Richardson <pa**@pnr-group.co.uk
wrote:
What i'm trying to do is assign a class (i.e. UKPostCode) to a propetyof
the UKAddress Class.
You may want to fill in some of the "..." in the code you posted, and bea
little more clear on what you're trying to do and what isn't working for
you. The basic code you posted, inasmuch as we can see any of it, seems
fine, and you can easily use a public property with a getter and setter to
set the internal storage (like a field).

But the code you posted _seems_ to do that. So what's wrong?
[...]
So i can do the following:

UKAddress address = new UKAddress();

address.PostCode.AMethod().
With the code you posted, you can do this. The getter of the PostCode
property returns (presumably) the reference to the UKPostCode instance
held by the UKAddress class, which you can then use to run the method
AMethod().
I know this can be done if i create a public field in the class as
UKPostCode, but that does not give may any checking with the set { ...}.
So far, you haven't posted any code where the setter would be used. Can
you be more explicit about what it is that you'd like the property setter
to do and what problem it is you're having in getting it to work?

Pete
Jul 5 '07 #2
I don't know if I am following correctly, but if you don't want people
messing with the postcode without the Address knowing about it, then
perhaps consider making the UKPostCode immutable - meaning no setters
or methods that allow you to manipulate it, short of creating a new
one for a new postcode. That way, the Address does know, because the
only way to change the postcode is to change the value of the
Address's Postcode property.
I'm a little unclear as to how many methods / properties you would
*need* on a postcode (and I've done quite a bit with GIS etc)...
personally I've never had to do more than use a string, performing
associated lookups (geocoding, etc) via a separate utility class that
accepts a string argument (so only used when necessary).

Marc

Jul 5 '07 #3

To further explain:

What i'm after is the way (correct way) of asigning a class to a class
property.

So for example:

UKAddress address = new UKAddress();
isPostCodeValid = address.PostCode.SetPostCode("NE1 4DU");
.....
Console.WriteLine("Post Code District: " + address.PostCode.District);
Console.WriteLine("Post Code Sector: " + address.PostCode.Sector);
....etc.

--------
What am after: The above example works fine if PostCode is a UKAddress field
variable (not a property).
Example:

public class UKAddress
{
public UKPostCode PostCode = new UKPostCode();
}
--------

What i trying to do, (tell me if i'm doing it wrong, as i'm still new with
the language) is:

public class UKAddress
{

private UKPostCode postCode = new UKPostCode();

public UKPostCode PostCode
{
get
{
return this.postCode;
}

set
{
this.postCode = value;
}
}
}
With this i get referancing errors when i do the following:

UKAddress address = new UKAddress();
isPostCodeValid = address.PostCode.SetPostCode("NE1 4DU");

I'm i surpose to using ref or out ??

Hope this helps

Paul R
Jul 5 '07 #4
On Jul 5, 2:25 pm, "Paul Richardson" <p...@pnr-group.co.ukwrote:

<snip>
With this i get referancing errors when i do the following:

UKAddress address = new UKAddress();
isPostCodeValid = address.PostCode.SetPostCode("NE1 4DU");

I'm i surpose to using ref or out ??
What *exactly* do you mean by "referencing errors"? It would greatly
help if you'd supply a short but complete program which demonstrates
the problem. See http://pobox.com/~skeet/csharp/complete.html

(I would personally consider making PostCode an immutable class, so
you'd create a new PostCode object and set that as the PostCode
property for an address...)

Jon

Jul 5 '07 #5
On Thu, 05 Jul 2007 06:25:35 -0700, Paul Richardson <pa**@pnr-group.co.uk
wrote:
[...]
With this i get referancing errors when i do the following:

UKAddress address = new UKAddress();
isPostCodeValid = address.PostCode.SetPostCode("NE1 4DU");

I'm i surpose to using ref or out ??
I don't see any practical difference between the two, other than of course
the nice encapsulation that using the property offers. If you are getting
an error, please post the _exact_ error message you are getting. From the
code you posted, I don't see anything that should generate an error, and
it seems like a fine way to do what you seem to be asking to do. Which
suggests that the code you posted is not actually the same as the code
you're trying to compile. :)

I also agree with Jon on his other two points: you may want to consider
making the UKPostCode class immutable, and for better assistance here you
should post a concise-but-complete sample of code that reliably reproduces
your problem.

With respect to making the class immutable, that would mean that you could
only construct a new UKPostCode instance, and the SetPostCode() method
would go away. You'd use the property for the UKAddress class only to get
or set the current UKPostCode instance, rather than getting it for the
purpose of modifying the instance.

Pete
Jul 5 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Peter King | last post by:
if you assign multiple classes in order to keep your classes generic e.g ..classA { position.absolute; left:5 top:5 height:200 width:800 } ..classB { background-color: red} ..classC {...
6
by: Jon Agiato | last post by:
Hello all, Here is the jist of my situation: I have an ArrayList of class A. class A contains a Stack of class B. I need to add instances of class B to the Stack in each instance of class A...
4
by: c | last post by:
Hello All, I am struggling with simple casting issue within c# and would really appreciate some insight into where I am going wrong. I have two classes, ClassA and ClassB. Each implements a...
3
by: MattC | last post by:
Hi, I found this code somewhere on the net and need to make some alterations. public class GenericSorter : IComparer { String sortProperty; bool sortOrder; public GenericSorter(String...
4
by: Steve Franks | last post by:
I have this cool nested master page scenario working great. However what is the correct way to be able to access a strongly typed property at the top level master from a content page that has a...
9
by: Joel Moore | last post by:
I'm a little confused here. If I have the following: Public ClassA Friend varA As Integer Private varB As Integer Private ClassB Public Sub MethodA() ' How can I access varA and varB here?...
1
by: John Dann | last post by:
I'm realising that there's something important that I don't understand about acccessing class properties. In fact I'm not even sure that I can explain clearly what it is that I don't understand!...
5
by: Cyril Gupta | last post by:
Hello, I have a class inside another class. The Scenario is like Car->Engine, where Car is a class with a set of properties and methods and Engine is another class inside it with its own set of...
5
by: =?Utf-8?B?bXVzb3NkZXY=?= | last post by:
Hi guys I'm trying to make my code as streamlined as possible, and add CSS file references dynamically when they are required, for example, if a page contains a webcontrol, then the related CSS...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.