473,545 Members | 937 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Confusing property names

I'm struggling with how to properly name a specific subset of Properties in
my classes. An example will illustrate it best. Let's say you have a class
named Person with the code below. Each Person instance has a single Address
instance. The Property name is Address, but it returns an instance of type
Address. This results in the code "public Address Address" below when
defining the property. It doesn't make sense to make the property name be
GetAddress since it has a GET and SET. Making the first character of the
property lower case would be confusing too since you could have a field or
variable called "address" which matched it.

Goofy - yes. Any recommendations ?

Note - this is just bogus data to make my point, I'm not actually working
with Person and Address classes.
public class Person
{
private string _firstName;
private Address _address;

public Person (firstName)
{
_firstName = firstName;
_address = new Address("123 Washington Dr.");
}

public Address Address
{
get { return _address;}
set { _address = value;}
}

public bool FooBar()
{
//Do something with the address.
Address address = Address;

//Yikes - this creates a local instance of address using the
property Address.
//I agree that this might not be good style regardless of the naming
convention, but it illustrates my problem well.
}

public string FirstName
{
get { return _firstName;}
set { _firstName = value; }
}
}
Nov 16 '05 #1
4 1573
"Mark" <fi******@idono tlikejunkmail.u mn.edu> wrote:
The Property name is Address, but it returns an instance
of type Address. This results in the code "public Address
Address" below when defining the property.


I think that's acceptable. In fact, the standard .NET classes have
this kind of thing: consider Pen.Color and Control.Size.

Your line ...
Address address = Address;
.... seems a bit pointless, given that a class does not need to hide
information from itself (and therefore does not need to use its own
properties), but anyway I'd prefer ...
Address address = this.Address;

P.
Nov 16 '05 #2
Mark,

I disagree with Paul. Writing maintainable code is hard enough
without using the same identifier for a type name and variable name.

In the rare cases where this arises in my code, I rename the type to
something like "AddressCla ss" or "AddressStruct" .

--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 16 '05 #3
"Chris R. Timmons" <crtimmons@X_NO SPAM_Xcrtimmons inc.com> wrote in message
news:Xn******** *************** ***********@207 .46.248.16...
Mark,

I disagree with Paul. Writing maintainable code is hard enough
without using the same identifier for a type name and variable name.


And I disagree with you, ;). Types and properties which happen to share a
name are going to happen in any halfway clear design and postfixing a class
type to the actual type name has potential issues(such as the type name
having to change if the type usage changes and "what the hell was the
designer thinking" questions). You simply cannot realistically try to keep
your type names from conflicting with property names in all cases, you might
be able to avoid a property that has the same identifier for its name and
type by mangling your type names, but you will not achieve it globally
without throughly confusing your code.

I'd rather deal with common identifiers over resorting to prefixes or
postfixes to avoid potential conflicts.
Nov 16 '05 #4
I have to say that Microsoft once said (maybe in C# language reference?)
that this is the way to do it...

public Adress Adress
{
get{...}
set{...}
}

this is regardless of the question if this is good or bad...
"Mark" <fi******@idono tlikejunkmail.u mn.edu> wrote in message
news:%2******** ********@TK2MSF TNGP14.phx.gbl. ..
I'm struggling with how to properly name a specific subset of Properties
in
my classes. An example will illustrate it best. Let's say you have a
class
named Person with the code below. Each Person instance has a single
Address
instance. The Property name is Address, but it returns an instance of
type
Address. This results in the code "public Address Address" below when
defining the property. It doesn't make sense to make the property name
be
GetAddress since it has a GET and SET. Making the first character of the
property lower case would be confusing too since you could have a field or
variable called "address" which matched it.

Goofy - yes. Any recommendations ?

Note - this is just bogus data to make my point, I'm not actually working
with Person and Address classes.
public class Person
{
private string _firstName;
private Address _address;

public Person (firstName)
{
_firstName = firstName;
_address = new Address("123 Washington Dr.");
}

public Address Address
{
get { return _address;}
set { _address = value;}
}

public bool FooBar()
{
//Do something with the address.
Address address = Address;

//Yikes - this creates a local instance of address using the
property Address.
//I agree that this might not be good style regardless of the
naming
convention, but it illustrates my problem well.
}

public string FirstName
{
get { return _firstName;}
set { _firstName = value; }
}
}

Nov 16 '05 #5

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

Similar topics

6
11539
by: manuel | last post by:
Exist a sintax like import Foo for property in Foo print property ?
10
1969
by: crjunk | last post by:
I have a script that I want to run only when my input box IS NOT disabled. Can someone tell me if something is wrong with my script? Is "disabled" the correct property to use? function TextChanged(i){ if (!document.ScheduleForm.disabled) { document.ScheduleForm.txtRecordStatus.value = "Changes Made; Record Not Saved.";...
3
3555
by: Kent Eilers | last post by:
I want to follow naming conventions for my controls - i usually prefix combo boxes with "cbo". When a form is in datasheet view however i do not want the user to see the 'cbo' prefix in front of the field name. Is there a way to retain this nomenclature and yet have the view display the field name (or anyname i want for that matter)? ...
5
2074
by: pembed2003 | last post by:
Hi all, I am reading the book "C How to Program" and in the chapter where it discuss scope rule, it says there are four scopes for a variable: function scope file scope block scope function-prototype scope I think(might be wrong):
0
963
by: | last post by:
Hello, I'm trying to write a function that will display, in a message box, the enumerated property names and values of the passed in Win32 class name. The function is as follows: Function displayClassPropertyNames(ByVal className As String) Dim mc As New ManagementClass(className) Dim pdc As PropertyDataCollection = mc.Properties Dim pd...
2
2026
by: Jon Miller | last post by:
Hi, I created a web service using Java/JAX-WS. I'm able to consume this web service using VS 2005 and .NET without a problem. However, the class and property names that were generated for the proxy are in camel case. i.e. the first letter is lower case rather than upper case. Does anyone know how to correct this (short of manually renaming...
4
2797
by: John Allen | last post by:
Hi there, Does anyone know if the standard "PropertyGrid" control is (foreign) language sensitive. If I display an object in the control, and my object has the native .NET "Size" struct as a property (which also contains its own ""Width" and "Height" members), will someone running my app on a German version of Windows for instance still...
7
1356
by: Andy B | last post by:
I saw this in the set accessor of a property: Set(ByVal value As DataSet) What exactly does the stuff in the () mean? VS complained about it not being there when I took it out not knowing it needed to be there.
18
1630
by: David Moss | last post by:
Hi, I want to manage and control access to several important attributes in a class and override the behaviour of some of them in various subclasses. Below is a stripped version of how I've implemented this in my current bit of work. It works well enough, but I can't help feeling there a cleaner more
0
7401
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
7656
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. ...
0
7807
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7419
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...
1
5326
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
3442
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1879
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1014
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
703
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.