By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,723 Members | 828 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,723 IT Pros & Developers. It's quick & easy.

strange compile when using inheritance

P: n/a
Hello!

My question is about calling this method CollectData below but I get a
compile error that I shouldn't have because the type parameter is correct.
The compile error is the following:
C:\PK\Development\Products\UTCAS\4.0\SRC\MeltPracA pplication\Dialog\Composit
ionForm.cs(942): Argument '1': cannot convert from 'ref
MeltPracData.MeltPracDataComposition' to 'ref
MeltPracCommon.IDialogPostData'

public bool CollectData(ref IDialogPostData post)
{...}

This IDialogPostData is an interface of type
MeltPracCommon.IDialogPostData.
We have an abstract class called MeltPracData which has the following
definition
public abstract class MeltPracData : IDialogPostData
{...}
As you can see this MeltPracData implements IDialogPostData

I have another class called MeltPracDataComposition which has the following
definition.
public class MeltPracDataComposition : MeltPracData
{...}
As you can see this MeltPracDataComposition is an MeltPracData according to
general inheritance rules.

In my program I have these two statement
MeltPracDataComposition mpd = new MeltPracDataComposition();
CollectData(ref mpd);
which cause compile error.
In my opinion these two statements shouldn't cause any compile error.

Some comment why these two statements shouldn't cause any compile error.
First of all I instansiate an MeltPracDataComposition which is an
MeltPracData
and a MeltPracData implement IDialogPostData.
This mean that I can send an actual parameter of type
MeltPracDataComposition
and receive it as an formal parameter of type IDialogPostData.

Another strange thing is if a remove the ref in the CollectData and
call it in this way CollectData(mpd);
I don't get any compile error.

I know what ref means but I can't figure out why I get a compile error when
having this ref in the method definition for collectData.

//Tony
Sep 13 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Since the "post" param to "CollectData" is ref, so "CollectData" can
update it. And "CollectData" is free to assign anything that is an
IDialogPostData. Such as "MyOtherClass : IDialogPostData". This means
that the compiler cannot verify the integrity of your "mpd" variable.

In most cases, "ref" is used incorrectly for parameters; do you
*really* need to re-assign the variable within the method? Or is this
intended to support reference-semantics for structs that support this
interface?

Anyway, you can probably fix it (keeping the ref) by:
IDialogPostData iparam = mpd;
CollectData(iparam);
mpd = (MeltPracDataComposition) iparam;

The easier fix is to remove the "ref".

Marc

Sep 13 '06 #2

P: n/a
Hello!!

I don't understand you.
I mean because MeltPracDataComposition is an IDialogPostData it should work.

Can you explain in another way perhaps.

//Tony

"Marc Gravell" <ma**********@gmail.comskrev i meddelandet
news:11*********************@p79g2000cwp.googlegro ups.com...
Since the "post" param to "CollectData" is ref, so "CollectData" can
update it. And "CollectData" is free to assign anything that is an
IDialogPostData. Such as "MyOtherClass : IDialogPostData". This means
that the compiler cannot verify the integrity of your "mpd" variable.

In most cases, "ref" is used incorrectly for parameters; do you
*really* need to re-assign the variable within the method? Or is this
intended to support reference-semantics for structs that support this
interface?

Anyway, you can probably fix it (keeping the ref) by:
IDialogPostData iparam = mpd;
CollectData(iparam);
mpd = (MeltPracDataComposition) iparam;

The easier fix is to remove the "ref".

Marc

Sep 13 '06 #3

P: n/a
Hi,

Why are you using ref in the first place?

The compiler is correct , the reason is a little difficult to explain
though.

Let's say that you have
interface I{}
class A:I{}
class B:I{}

both A & B implement I but they have no relationship between them.

You can have:
method ( I i ){

i = new B();
i = new A();
}

it does not matter if I originally had an instance of A or B

but when you have:
method ( ref I i ){

i = new B();
}

and you call it like
method( new A() );

you would end up with i that contained originally a reference to A being
assigned to a reference to B

This is clearly an error and that's why the compiler complains
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"tony" <jo*****************@telia.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Hello!

My question is about calling this method CollectData below but I get a
compile error that I shouldn't have because the type parameter is correct.
The compile error is the following:
C:\PK\Development\Products\UTCAS\4.0\SRC\MeltPracA pplication\Dialog\Composit
ionForm.cs(942): Argument '1': cannot convert from 'ref
MeltPracData.MeltPracDataComposition' to 'ref
MeltPracCommon.IDialogPostData'

public bool CollectData(ref IDialogPostData post)
{...}

This IDialogPostData is an interface of type
MeltPracCommon.IDialogPostData.
We have an abstract class called MeltPracData which has the following
definition
public abstract class MeltPracData : IDialogPostData
{...}
As you can see this MeltPracData implements IDialogPostData

I have another class called MeltPracDataComposition which has the
following
definition.
public class MeltPracDataComposition : MeltPracData
{...}
As you can see this MeltPracDataComposition is an MeltPracData according
to
general inheritance rules.

In my program I have these two statement
MeltPracDataComposition mpd = new MeltPracDataComposition();
CollectData(ref mpd);
which cause compile error.
In my opinion these two statements shouldn't cause any compile error.

Some comment why these two statements shouldn't cause any compile error.
First of all I instansiate an MeltPracDataComposition which is an
MeltPracData
and a MeltPracData implement IDialogPostData.
This mean that I can send an actual parameter of type
MeltPracDataComposition
and receive it as an formal parameter of type IDialogPostData.

Another strange thing is if a remove the ref in the CollectData and
call it in this way CollectData(mpd);
I don't get any compile error.

I know what ref means but I can't figure out why I get a compile error
when
having this ref in the method definition for collectData.

//Tony


Sep 13 '06 #4

P: n/a
tony wrote:
I don't understand you.
I mean because MeltPracDataComposition is an IDialogPostData it should work.
No, it shouldn't. "ref" parameters need to be *exactly* the right type.

Consider the following code:

public void MakeAnObject (ref object foo)
{
foo = new object();
}

string x = "hello";
MakeAnObject (ref x);

This is illegal, because it ends up with the variable x being a
reference to an object, not a string.

See http://www.jaggersoft.com/csharp_standard/14.4.2.1.htm

<quote>
A function member is said to be an applicable function member with
respect to an argument list A when all of the following are true:

* 2 The number of arguments in A is identical to the number of
parameters in the function member declaration.
* 3 For each argument in A, the parameter passing mode of the
argument (i.e., value, ref, or out) is identical to the parameter
passing mode of the corresponding parameter, and
o 4 for a value parameter or a parameter array, an implicit
conversion (13.1) exists from the type of the argument to the type of
the corresponding parameter, or
o 5 for a ref or out parameter, the type of the argument is
identical to the type of the corresponding parameter.
</quote>

The last sentence is the important bit.

Jon

Sep 13 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.