Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

C# Programming

Newbie Q: Explain: string Name = dept.FindChairperson().Name


Hi All,
Quick newbie question: I am NOT looking for some code solution - I am
simply looking for an explanation.  In a passing bit of code in a C#
book I have, I saw the following line:

string Name = dept.FindChairperson().Name

I AM pretty much getting C# and its various syntaxes, but I had not
seen this before.  Here's my question:  It is clear that
"FindChairperson" is a method within the dept object.  But then
how is ".Name" somehow subservient to the method?  In other words,
I think of a class as having methods and properties... but how is it
that they are somehow COMBINED here?

Thanks much,

Rex

This method returns object with property Name

HTH
Alex

"Rex" <RexForum4@OneSimpleHabit.com> wrote in message

news:iima63pouvcch63c36mj27pcikcbhangm9@4ax.com...

On Jun 5, 1:50 pm, Rex <RexForum4@OneSimpleHabit.com> wrote:

> Quick newbie question: I am NOT looking for some code solution - I am
> simply looking for an explanation.  In a passing bit of code in a C#
> book I have, I saw the following line:

> string Name = dept.FindChairperson().Name

> I AM pretty much getting C# and its various syntaxes, but I had not
> seen this before.  Here's my question:  It is clear that
> "FindChairperson" is a method within the dept object.  But then
> how is ".Name" somehow subservient to the method?  In other words,
> I think of a class as having methods and properties... but how is it
> that they are somehow COMBINED here?

Okay: dept is a variable, of some type that has a FindChairperson()
method. The return type of the FindChairperson method then has a Name
property. So FindChairperson() might be declared as something like:

Person FindChairperson()

where Person has a Name property.

So your original expression calls FindChairperson and then uses the
Name property of the Person returned by that method.

(I'm deliberately ignoring the fact that it's probably a reference
which is returned, etc, in order to keep things simple.)

Jon

-----------------------------------------------Reply-----------------------------------------------

This is called chaining.

FindChairperson() returns another object, probably a Person or
ChairPerson object, on which the Name property is called. You could
rewrite this as:

ChairPerson chairperson = dept.FindChairperson();
string Name = chairperson.Name;

Often when the intermediate object (chairperson in this case) is not
needed the method/property calls are chained, ie written one after the
other without storing the intermediate objects. Precedence rules make
the "dot" work on the result of the last method call or property in the
chain.

Greetz,

-- Freddy

Rex schreef:

"Rex" <RexForum4@OneSimpleHabit.com> schrieb im Newsbeitrag
news:iima63pouvcch63c36mj27pcikcbhangm9@4ax.com...

> Hi All,
> Quick newbie question: I am NOT looking for some code solution - I am
> simply looking for an explanation.  In a passing bit of code in a C#
> book I have, I saw the following line:

> string Name = dept.FindChairperson().Name

> I AM pretty much getting C# and its various syntaxes, but I had not
> seen this before.  Here's my question:  It is clear that
> "FindChairperson" is a method within the dept object.  But then
> how is ".Name" somehow subservient to the method?

The return type of FindChairperson has a property Name of type string (or a
type that is implicitly convertible to string).

I suppose it is a class representing in person and Name is the name of that
person.

To know more, you have to look at the definition resp. documentation of the
type of dept. Maybe the intellisense tells you more.

hth
Christof

-----------------------------------------------Reply-----------------------------------------------

Thanks, Alex - But why wouldn't it just be:

string Name = dept.FindChairperson()

But maybe my REAL question is: How would

      dept.FindChairperson().Name

actually be coded within the dept class (so that a method is combined
with a property)?

Thanks,

Rex

On Tue, 5 Jun 2007 08:54:28 -0400, "AlexS"

Name property isn't coded within dept class. It is coded in the class, which
is returned by method.

"Rex" <RexForum4@OneSimpleHabit.com> wrote in message

news:1jna6353v2ns0n6idhndnmfvf0q364rom3@4ax.com...

HEY, thanks everybody -  I think I got it!
The method is returning an object - and  that OBJECT has a property
called "Name".  All the explanations together helped -and  Freddy, I
think your description of this being a "chain" especially helped.
Thanks very much - I really appreciate the excellent explanation,
everyone.
Rex

On Tue, 05 Jun 2007 08:50:03 -0400, Rex

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc