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

  • Thread starter Thread starter Paul Richardson
  • Start date Start date
P

Paul Richardson

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
 
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
 
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
 
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
 
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
 
[...]
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top