From: Gordon Padwick on
I hope Santa Claus was kind to you all!

Back to business:

As part of my on-going task of becoming familiar with C#, I got into the
IEnumerable interface and looked carefully into the sample code Microsoft
provides in the relevant Visual Studio help topic. Within that sample code,
I find the lines:

public bool MoveNext()
{
position++
return (position < _people.Length);
}

Because the code didn't seem to be using any return value from the MoveNext
method, I commented out the return line. When I subsequently built the code,
I received the error message that said "not all code paths return a value."

What is going on here? If I'm not using a return value, why do I have to
return one?

As always, I'll appreciate all responses

Gordon

From: Igor Tandetnik on
Gordon Padwick wrote:
> public bool MoveNext()
> {
> position++
> return (position < _people.Length);
> }
>
> Because the code didn't seem to be using any return value from the MoveNext
> method, I commented out the return line. When I subsequently built the code,
> I received the error message that said "not all code paths return a value."
>
> What is going on here?

The function is declared to return a bool, so you have to return a bool. The calling code is then free to ignore the return value if it so chooses.

Note that the compiler cannot possibly know whether the return value may be used by someone somewhere. For all it knows, your assembly may be loaded by some client at runtime and the method called via reflection.

> If I'm not using a return value, why do I have to
> return one?

Because function signature requires you to.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925

From: Gordon Padwick on
Thanks for the clarification

Gordon

"Igor Tandetnik" <itandetnik(a)mvps.org> wrote in message
news:ON3L9gehKHA.5568(a)TK2MSFTNGP02.phx.gbl...
Gordon Padwick wrote:
> public bool MoveNext()
> {
> position++
> return (position < _people.Length);
> }
>
> Because the code didn't seem to be using any return value from the
> MoveNext
> method, I commented out the return line. When I subsequently built the
> code,
> I received the error message that said "not all code paths return a
> value."
>
> What is going on here?

The function is declared to return a bool, so you have to return a bool. The
calling code is then free to ignore the return value if it so chooses.

Note that the compiler cannot possibly know whether the return value may be
used by someone somewhere. For all it knows, your assembly may be loaded by
some client at runtime and the method called via reflection.

> If I'm not using a return value, why do I have to
> return one?

Because function signature requires you to.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily
a good idea. It is hard to be sure where they are going to land, and it
could be dangerous sitting under them as they fly overhead. -- RFC 1925

From: Gordon Padwick on
Please help me understand this in more detail

The code example in Visual Studio help includes:

public class PeopleEnum : IEnumerator
{
.. .
public bool MoveNext()
{
position++;
return (position < _People.Length);
}
. . .
}

My understanding is that the PeopleEnum class is derived from the
IEnumerator class of which the members include the MoveNext method with a
return type of bool.

So, in the class PeopleEnum class definition, why is it necessary to again
define the MoveNext method?

The usage in this example seems to suggest that if I create a class derived
from another class, I can simply replace a method in the parent class with
another method. Is that correct, or is more involved?

Thanks for helping me gain an understanding of C#.

Gordon



"Gordon Padwick" <gpadwick(a)earthlink.net> wrote in message
news:%23684NlohKHA.1456(a)TK2MSFTNGP06.phx.gbl...
> Thanks for the clarification
>
> Gordon
>
> "Igor Tandetnik" <itandetnik(a)mvps.org> wrote in message
> news:ON3L9gehKHA.5568(a)TK2MSFTNGP02.phx.gbl...
> Gordon Padwick wrote:
>> public bool MoveNext()
>> {
>> position++
>> return (position < _people.Length);
>> }
>>
>> Because the code didn't seem to be using any return value from the
>> MoveNext
>> method, I commented out the return line. When I subsequently built the
>> code,
>> I received the error message that said "not all code paths return a
>> value."
>>
>> What is going on here?
>
> The function is declared to return a bool, so you have to return a bool.
> The calling code is then free to ignore the return value if it so chooses.
>
> Note that the compiler cannot possibly know whether the return value may
> be used by someone somewhere. For all it knows, your assembly may be
> loaded by some client at runtime and the method called via reflection.
>
>> If I'm not using a return value, why do I have to
>> return one?
>
> Because function signature requires you to.
> --
> With best wishes,
> Igor Tandetnik
>
> With sufficient thrust, pigs fly just fine. However, this is not
> necessarily a good idea. It is hard to be sure where they are going to
> land, and it could be dangerous sitting under them as they fly
> overhead. -- RFC 1925
>

From: Igor Tandetnik on
Gordon Padwick wrote:
> public class PeopleEnum : IEnumerator
> {
> .. .
> public bool MoveNext()
> {
> position++;
> return (position < _People.Length);
> }
> . . .
> }
>
> My understanding is that the PeopleEnum class is derived from the
> IEnumerator class

To be precise, PeopleEnum implements IEnumerator interface.

> of which the members include the MoveNext method with a
> return type of bool.
>
> So, in the class PeopleEnum class definition, why is it necessary to again
> define the MoveNext method?

An interface declares some methods. A class that wants to implement the interface needs to implement all its methods.

If you are familiar with C++, a C# interface is similar to a C++ class with no data members, and all methods being pure virtual. If you are familiar with COM, a C# interface is similar to a COM interface.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
 |  Next  |  Last
Pages: 1 2
Prev: C++ and VC++ Differences
Next: std::map question