473,394 Members | 1,854 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,394 software developers and data experts.

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 1567
"Mark" <fi******@idonotlikejunkmail.umn.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 "AddressClass" or "AddressStruct".

--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 16 '05 #3
"Chris R. Timmons" <crtimmons@X_NOSPAM_Xcrtimmonsinc.com> wrote in message
news:Xn**********************************@207.46.2 48.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******@idonotlikejunkmail.umn.edu> wrote in message
news:%2****************@TK2MSFTNGP14.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
by: manuel | last post by:
Exist a sintax like import Foo for property in Foo print property ?
10
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...
3
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...
5
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...
0
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...
2
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...
4
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...
7
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...
18
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
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...
0
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...
0
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...

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.