
Signature
Joanna Carter [TeamB]
Consultant Software Engineer
> | I think I had a problem with using out parameter , why the instance of
> | 'SubClass' can't convert to 'BaseClass' ? my code is :
>
> I think this could be a misleading message; there is definitely a problem
> with the following :
It's not a misleading message - it's just that there are multiple
things wrong with the code.
> | static void Test( out BaseClass y )
> | {
[quoted text clipped - 6 lines]
> The out parameter means that the reference passed in *has* to be initialised
> as it is assumed to be null.
No - it's not assumed to be null. It's not definitely assigned. Big
difference :) If it were assumed to be null, you wouldn't get a
compile-time error, you'd get a NullReferenceException at run-time.
> The purpose of the out modifier is to allow the
> same funtionality as returning a reference from a function and should be
> treated as such. Its purpose is to return references from a method not to
> pass references into it.
Indeed. The reason the OP is getting the error message, however, is
that the actual argument must be a variable of exactly the same type as
the output parameter.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Joanna Carter [TeamB] - 22 Dec 2005 08:24 GMT
| No - it's not assumed to be null. It's not definitely assigned. Big
| difference :) If it were assumed to be null, you wouldn't get a
| compile-time error, you'd get a NullReferenceException at run-time.
You are right, it's early and my brain isn't yet communicating clearly :-)
| > The purpose of the out modifier is to allow the
| > same funtionality as returning a reference from a function and should be
[quoted text clipped - 4 lines]
| that the actual argument must be a variable of exactly the same type as
| the output parameter.
Agreed. I think the problem is that the OP does not yet understand what the
out modifier actually does.
private void TestOut(out BaseClass value)
{
value.Test();
}
This code actually raise two errors :
Error 1 Use of unassigned out parameter 'value' F:\Test\Form1.cs 31 7 Test
Error 2 The out parameter 'value' must be assigned to before control leaves
the current method F:\Test\Form1.cs 29 18 Test
So the OP's examples definitely won't compile for other reasons than the
faulty parameter type. These errors make it clear that an out parameter
*must* be assigned something and not used to pass something in.
Can I wake up now ? :-)
Joanna

Signature
Joanna Carter [TeamB]
Consultant Software Engineer
Jon Skeet [C# MVP] - 22 Dec 2005 08:53 GMT
> | No - it's not assumed to be null. It's not definitely assigned. Big
> | difference :) If it were assumed to be null, you wouldn't get a
> | compile-time error, you'd get a NullReferenceException at run-time.
>
> You are right, it's early and my brain isn't yet communicating clearly :-)
No problem. I suspected you probably realised that, but thought it
would be worth clarifying it for the sake of everyone else :)
> | > The purpose of the out modifier is to allow the
> | > same funtionality as returning a reference from a function and should be
[quoted text clipped - 8 lines]
> Agreed. I think the problem is that the OP does not yet understand what the
> out modifier actually does.
Quite possibly. The reason you can't use an out argument of a derived
type is probably the most subtle part of it, to be honest.
> private void TestOut(out BaseClass value)
> {
[quoted text clipped - 10 lines]
> faulty parameter type. These errors make it clear that an out parameter
> *must* be assigned something and not used to pass something in.
Yup.
> Can I wake up now ? :-)
Yup - go get some coffee :)

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too