473,738 Members | 9,555 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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\Developme nt\Products\UTC AS\4.0\SRC\Melt PracApplication \Dialog\Composi t
ionForm.cs(942) : Argument '1': cannot convert from 'ref
MeltPracData.Me ltPracDataCompo sition' 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 MeltPracDataCom position which has the following
definition.
public class MeltPracDataCom position : MeltPracData
{...}
As you can see this MeltPracDataCom position is an MeltPracData according to
general inheritance rules.

In my program I have these two statement
MeltPracDataCom position mpd = new MeltPracDataCom position();
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 MeltPracDataCom position which is an
MeltPracData
and a MeltPracData implement IDialogPostData .
This mean that I can send an actual parameter of type
MeltPracDataCom position
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 1843
Since the "post" param to "CollectDat a" is ref, so "CollectDat a" can
update it. And "CollectDat a" is free to assign anything that is an
IDialogPostData . Such as "MyOtherCla ss : 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(ipa ram);
mpd = (MeltPracDataCo mposition) iparam;

The easier fix is to remove the "ref".

Marc

Sep 13 '06 #2
Hello!!

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

Can you explain in another way perhaps.

//Tony

"Marc Gravell" <ma**********@g mail.comskrev i meddelandet
news:11******** *************@p 79g2000cwp.goog legroups.com...
Since the "post" param to "CollectDat a" is ref, so "CollectDat a" can
update it. And "CollectDat a" is free to assign anything that is an
IDialogPostData . Such as "MyOtherCla ss : 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(ipa ram);
mpd = (MeltPracDataCo mposition) 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.com wrote in message
news:%2******** ********@TK2MSF TNGP03.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\Developme nt\Products\UTC AS\4.0\SRC\Melt PracApplication \Dialog\Composi t
ionForm.cs(942) : Argument '1': cannot convert from 'ref
MeltPracData.Me ltPracDataCompo sition' 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 MeltPracDataCom position which has the
following
definition.
public class MeltPracDataCom position : MeltPracData
{...}
As you can see this MeltPracDataCom position is an MeltPracData according
to
general inheritance rules.

In my program I have these two statement
MeltPracDataCom position mpd = new MeltPracDataCom position();
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 MeltPracDataCom position which is an
MeltPracData
and a MeltPracData implement IDialogPostData .
This mean that I can send an actual parameter of type
MeltPracDataCom position
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 MeltPracDataCom position 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
3126
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 absolute address of alpha to access to it. What confuses me is that when the variable is dynamically allocated, how does the compiler implement it? We know the address of the variable until run-time. During the compilation, how can we access to the...
3
1762
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 findng that in my method definitions, I am having to fully scope the names of any other functions I am calling. I do not understand this. The clas declaration code look some thing like this: class A: public Singleton<A> { public:
2
3465
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 and the Employee class. I have removed all kind of data from all classes. The compile error occur when I instansiate Person* p = new TeachingAssistent; The error I get is c:\Documents and
13
1487
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
1770
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 the individual elements in an array of ImageList's could be identified by the ID, thereby allowing re-ordering the array without harm. A person could identify by index into the array, but that would not be preserved by re-ordering (and re-ordering...
1
2300
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 value of s is "test2.icon1.ico". This program will compile and run just fine from visual studio .net 2003 using .net 1.1 and also will compile and run just fine using csc form1.cs /resource:icon1.ico. Now, I created a new string called s2 and...
6
2362
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 problem also with other imports (e.g. urllib2 etc.). And again very often in all these cases where I get weired Python exceptions, the problem is around re-functions - usually during re.compile calls during import (see some of the exceptions below). But...
16
5427
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 first be know at runtime. But is there some strict definitions that defines runtime code and compile time code that can be used in general?
6
1087
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 CollectionBase for member List it return an IList. In the class Int16Collection I check by using method Test what this List in CollectionBase is actually refering to and
0
8969
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8788
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9476
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9263
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.