473,395 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

strange compile when using inheritance

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
4 1824
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: newbiecpp | last post by:
I have hard time to understand run-time environment. Let assume that I have a program that has a simple variable alpha. When this variable is statically allocated, the compiler can use the...
3
by: Alfonso Morra | last post by:
I have some code that I am porting over from C. It is full of static functions and global variables. I have got around this by wrapping most of the code in a singleton object. However, I am...
2
by: Tony Johansson | last post by:
Hello Experts! I have been playing around a bit with this multiple inheritance and I wonder why do I get a compile error if I remove the virtual keyvord from the declaration of the Student class...
13
by: Patricia | last post by:
First, I know the following code is bad, but it's from a library I have to use, and I can't change it. class A { // some primitive members }; class B { // some primitive members };
6
by: Peter Oliphant | last post by:
I just discovered that the ImageList class can't be inherited. Why? What could go wrong? I can invision a case where someone would like to add, say, an ID field to an ImageList, possible so that...
1
by: hansolox1 | last post by:
The following program simply sets the icon of a form called form1. When I get the name of the embedded icon using GetManifestResourceNames(), I store the name in a string variable called s. The...
6
by: robert | last post by:
I get python crashes and (in better cases) strange Python exceptions when (in most cases) importing and using cookielib lazy on demand in a thread. It is mainly with cookielib, but remember the...
16
by: desktop | last post by:
I have read that using templates makes types know at compile time and using inheritance the types are first decided at runtime. The use of pointers and casts also indicates that the types will...
6
by: Tony Johansson | last post by:
Hello! Assume I have this inheritance see below. I know that a much better alternative is to use generics but I just want to know how things works. According to the documentation in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.