473,772 Members | 2,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.PostCod e.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 1504
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.PostCod e.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.PostCod e.SetPostCode(" NE1 4DU");
.....
Console.WriteLi ne("Post Code District: " + address.PostCod e.District);
Console.WriteLi ne("Post Code Sector: " + address.PostCod e.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.PostCod e.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.ukwrot e:

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

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

I'm i surpose to using ref or out ??
What *exactly* do you mean by "referencin g 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.PostCod e.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
5666
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 { background-color: green} <div id="div1" class="classA classB" ... but then want to dynamically assign a new class
6
2865
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 within the ArrayList like so (except correct, hehe): classA.stackInA.add(new classB(blah)); The problem is, this syntax gives me the following error:
4
1701
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 "public static implicit operator" to allow silent casting from one to the other. This works fine, per the documents I have read. However, I am unable to do this:
3
5154
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 sortBy, bool asc) {
4
2469
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 nested master page between the content page and the top level master? For example, assume the very top level master is called CompanyWide.master. And the nested master page is called DepartmentA.master, which inherits from CompanyWide master. ...
9
3688
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? End Sub
1
2519
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! But let me try: Let's say that I have a specific parameter value that needs to be available to several different classes within a program. What I'm doing currently is to store this specific value in a property of MyClass1. This class is then...
5
2706
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 properties. I want to know if there is a way to access the methods and the properties of the Owner class for the class that's inside it? I.e. I want to find out within Engine what make the Car is which is exposed by the property Car.Model.
5
2715
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 file is added by the webcontrol. This prevents me having to remember to add the CSS file to the page if I use a certain webcontrol. I have a MasterPage with an array of StyleSheets, and a public function for
0
9619
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10261
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10038
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
6713
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.