PC Review


Reply
Thread Tools Rate Thread

Confusing property names

 
 
Mark
Guest
Posts: n/a
 
      31st Dec 2004
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; }
}
}


 
Reply With Quote
 
 
 
 
Paul E Collins
Guest
Posts: n/a
 
      31st Dec 2004
"Mark" <(E-Mail Removed)> 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.


 
Reply With Quote
 
Chris R. Timmons
Guest
Posts: n/a
 
      31st Dec 2004
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/
 
Reply With Quote
 
Daniel O'Connell [C# MVP]
Guest
Posts: n/a
 
      31st Dec 2004
"Chris R. Timmons" <crtimmons@X_NOSPAM_Xcrtimmonsinc.com> wrote in message
news:Xns95D094CA3998Fcrtimmonscrtimmonsin@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.


 
Reply With Quote
 
Picho
Guest
Posts: n/a
 
      1st Jan 2005
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" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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; }
> }
> }
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Get/Set sections of a class property confusing... Andy B Microsoft VB .NET 7 30th May 2008 02:12 PM
How to set "List Font names in their names" property in Word 2007 Test Engineer Microsoft Word Document Management 4 10th Apr 2008 11:06 AM
Re: ManagementBaseObject property names Willy Denoyette [MVP] Microsoft Dot NET Framework 0 14th Sep 2004 07:16 PM
Re: ManagementBaseObject property names Imran Koradia Microsoft Dot NET Framework 0 14th Sep 2004 06:27 PM
PropertyGrid and property names Chris Dunaway Microsoft VB .NET 1 16th Aug 2004 06:55 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:14 AM.