Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / October 2007

Tip: Looking for answers? Try searching our database.

using access to a field from a passed object

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tony Johansson - 29 Oct 2007 23:12 GMT
Hello!

Below I have method MostPowerful  which is located in class SportsCar.
As you can see the fomel parameter for method MostPowerful is carCompare of
class SportsCar.
Because this method MostPowerful is a member of class SportsCar
can I use the field directly for the passed SportsCar object in
this way "carCompare.horsepower".

Now to my question. I have two choices. I wonder which is best according to
OOP.
1. Use the field directly as I have done by using  this statement
"carCompare.horsepower"
2 Or use a property to get the horsepower. Like carCompare.GetHorsepower or
somthing similar

Is the performance between using 1 or 2 about the same?

public SportsCar MostPowerful(SportsCar  carCompare)
{
   if (carCompare.horsepower > this.horsepower)
       return carCompare;
  else
     return this;
}

//Tony
Peter Duniho - 30 Oct 2007 00:38 GMT
> [...]
> Now to my question. I have two choices. I wonder which is best according to
[quoted text clipped - 5 lines]
>
> Is the performance between using 1 or 2 about the same?

It should be identical, assuming your property getter is just something
like "return horsepower;", since it would be inlined.

As far as which is better, I would generally prefer to use the property
to access the field.  Presumably the code comparing the two values
doesn't or shouldn't care about the implementation of the property, so
using the underlying field seems unnecessary and potentially
restrictive.

I would never name a property with the word "Get", for a variety of
reasons but the most obvious being that a property can be both assigned
and retrieved (set and get).  It would look kind of odd to have code
that reads like "carCompare.GetHorsepower = horsepowerNew;".  Just name
the property something that describes what it is, like "Horsepower".

Pete
Chris Shepherd - 30 Oct 2007 13:18 GMT
> I would never name a property with the word "Get", for a variety of
> reasons but the most obvious being that a property can be both assigned
> and retrieved (set and get).  It would look kind of odd to have code
> that reads like "carCompare.GetHorsepower = horsepowerNew;".  Just name
> the property something that describes what it is, like "Horsepower".

Actually, Microsoft's guidelines on how to name Type members basically
says don't do this -- in other words, you should use verbs/verb phrases
for methods, and noun/noun phrases/adjectives for Properties.

http://msdn2.microsoft.com/en-us/library/ms229012.aspx

Chris.
bryan - 30 Oct 2007 13:53 GMT
>> I would never name a property with the word "Get", for a variety of
>> reasons but the most obvious being that a property can be both assigned
[quoted text clipped - 7 lines]
>
> http://msdn2.microsoft.com/en-us/library/ms229012.aspx

Hmm.  Are you implying that the Microsoft guidelines say NOT to call the
property "Horsepower", or are you agreeing with Peter?

Horsepower is a noun, so it is perfectly suited for use as a Property name
in the guidelines to which you referred.
Chris Shepherd - 30 Oct 2007 14:03 GMT
>>> I would never name a property with the word "Get", for a variety of
>>> reasons but the most obvious being that a property can be both assigned
[quoted text clipped - 12 lines]
> Horsepower is a noun, so it is perfectly suited for use as a Property name
> in the guidelines to which you referred.

Sorry, I had re-written some of that for clarity and actually
accomplished the reverse (failed to remove "actually"). I was agreeing
with Peter.

Chris.
bryan - 30 Oct 2007 13:57 GMT
Make and use a property.  That way if you change the nature of the field but
can still obtain and return the Horsepower value, the property can still
return the value and users of the class won't need to change thier code.

> 1. Use the field directly as I have done by using  this statement
> "carCompare.horsepower"
> 2 Or use a property to get the horsepower. Like carCompare.GetHorsepower
> or
> somthing similar

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.